19
votes

I want to run multiple functions when I click a button. For example I want my button to look like

self.testButton = Button(self, text = "test", 
                         command = func1(), command = func2())

when I execute this statement I get an error because I cannot allocate something to an argument twice. How can I make command execute multiple functions.

9

9 Answers

29
votes

You could create a generic function for combining functions, it might look something like this:

def combine_funcs(*funcs):
    def combined_func(*args, **kwargs):
        for f in funcs:
            f(*args, **kwargs)
    return combined_func

Then you could create your button like this:

self.testButton = Button(self, text = "test", 
                         command = combine_funcs(func1, func2))
49
votes

You can simply use lambda like this:

self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
16
votes
def func1(evt=None):
    do_something1()
    do_something2()
    ...

self.testButton = Button(self, text = "test", 
                         command = func1)

maybe?

I guess maybe you could do something like

self.testButton = Button(self, text = "test", 
                         command = lambda x:func1() & func2())

but that is really gross ...

5
votes

You can use the lambda for this:

self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
5
votes

Button(self, text="text", command=func_1()and func_2)

3
votes

I think the best way to run multiple functions by use lambda.

here an example:

button1 = Button(window,text="Run", command = lambda:[fun1(),fun2(),fun3()])
1
votes

I've found also this, which works for me. In a situation like...

b1 = Button(master, text='FirstC', command=firstCommand)
b1.pack(side=LEFT, padx=5, pady=15)

b2 = Button(master, text='SecondC', command=secondCommand)
b2.pack(side=LEFT, padx=5, pady=10)

master.mainloop()

... you can do...

b1 = Button(master, command=firstCommand)
b1 = Button(master, text='SecondC', command=secondCommand)
b1.pack(side=LEFT, padx=5, pady=15)

master.mainloop()

What I did was just renaming the second variable b2 the same as the first b1 and deleting, in the solution, the first button text (so only the second is visible and will act as a single one).

I also tried the function solution but for an obscure reason it don't work for me.

1
votes

this is a short example : while pressing the next button it will execute 2 functions in 1 command option

    from tkinter import *
    window=Tk()
    v=StringVar()
    def create_window():
           next_window=Tk()
           next_window.mainloop()

    def To_the_nextwindow():
        v.set("next window")
        create_window()
   label=Label(window,textvariable=v)
   NextButton=Button(window,text="Next",command=To_the_nextwindow)

   label.pack()
   NextButton.pack()
   window.mainloop()
0
votes

check this out, I have tried this method as,I was facing the same problem. This worked for me.

def all():
    func1():
        opeartion
    funct2():
        opeartion
    for i in range(1):
        func1()
        func2()

self.testButton = Button(self, text = "test", command = all)