Have multiple commands when button is pressed
116,714
Solution 1
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))
Solution 2
You can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Solution 3
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 ...
Solution 4
You can use the lambda for this:
self.testButton = Button(self, text = "test", lambda: [f() for f in [func1, funct2]])
Solution 5
Button(self, text="text", command=func_1()and func_2)
Related videos on Youtube
Author by
user1876508
Updated on July 27, 2022Comments
-
user1876508 almost 2 years
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.
-
PeterBB over 11 yearsDefining a function to do what you want is probably the best solution. Putting some of the logic within the button itself strikes me as icky, and a potential maintenance problem later.
-
adiga over 6 yearsWhile this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
-
Hrvoje T almost 6 yearsHow to do this if this
combine_funcs
is method in a class? If I writedef combine_funcs(self, *funcs):
I get an error `TypeError: my_first_func() takes 1 positional argument but 2 were given