Have multiple commands when button is pressed
Question:
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.
Answers:
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 …
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))
You can use the lambda for this:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
You can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Button(self, text="text", command=func_1()and func_2)
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.
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()
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()])
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)
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()
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.
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 …
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))
You can use the lambda for this:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
You can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Button(self, text="text", command=func_1()and func_2)
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.
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()
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()])
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)
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()