Reputation: 13172
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.
Upvotes: 23
Views: 145102
Reputation: 1
You can solve this issue by simply adding your second function at the end of the first function, but only passing the first function as a command. This will trigger both functions but following a sequence.
I like this approach as it allows for a greater flexibility since you can add custom logic to be followed before the second function is called.
PS: In your code you should avoid adding ( ) when binding your function with the command arg as you did.
def func1():
pass
#code to execute before func2() is called
def func2():
pass
#code of func2()
pass
#code after func2() is executed
Upvotes: 0
Reputation: 1
I was looking for different solution. One button doing two functions on two different clicks. Example START button - after click changes text to STOP and starts function. After second click stop function and changes text to START again. Tried this and it works. I do not know if it is ok or elegant or does not break any python rules (I am a lousy programmer :-) but it works.
from tkinter import *
root=Tk()
def start_proc():
print('Start')
myButton.configure(text='STOP',command=stop_proc)
def stop_proc():
print('stop')
myButton.configure(text='START',command=start_proc)
myButton=Button(root,text='START' ,command=start_proc)
myButton.pack(pady=20)
root.mainloop()
Upvotes: 0
Reputation: 1
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)
Upvotes: 0
Reputation: 51
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()])
Upvotes: 5
Reputation: 11
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()
Upvotes: 1
Reputation: 2269
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.
Upvotes: 1
Reputation: 654
You can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Upvotes: 62
Reputation: 151
You can use the lambda for this:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
Upvotes: 5
Reputation: 208405
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))
Upvotes: 31
Reputation: 113930
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 ...
Upvotes: 18