You can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
Answer from pradepghr on Stack OverflowYou can simply use lambda like this:
self.testButton = Button(self, text=" test", command=lambda:[funct1(),funct2()])
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))
Hi, i've already made a little program that presses (Ctrl+S) shortcut automatically every x seconds, and i want to add a timer to it, so i need to put two functions in one button, here is a smaller and simpler code just to get the idea:
from tkinter import *
def hello():
print("HELLO")
def bye():
print("BYE")
root = Tk()
Button(root, width=7, text='CLICK ME',
command=hello).pack()
root.mainloop()python - tkinter call two functions - Stack Overflow
Python Tkinter Multiple Commands - Stack Overflow
python 3.x - multiple commands for a tkinter button - Stack Overflow
python tkinter, how run two commands on click on one button? - Stack Overflow
Videos
you can use the sample way with lambda like this:
button = Button(text="press", command=lambda:[function1(), function2()])
Make a new function that calls both:
def o_and_t():
o()
t()
button = Button(admin, text='Press', command=o_and_t)
Alternatively, you can use this fun little function:
def sequence(*functions):
def func(*args, **kwargs):
return_value = None
for function in functions:
return_value = function(*args, **kwargs)
return return_value
return func
Then you can use it like this:
button = Button(admin, text='Press', command=sequence(o, t))
Just create a method that calls the two methods. Tere's no shame in creating an extra function for this. It's a much more maintainable solution that trying to cram a bunch of code into a lambda.
def on_restart(self):
self.restartExternal()
self.restartWinow.destroy()
buttonRestart = Button(..., command = self.on_restart)
Instead of self.restartExternal() and restartWindow.destroy you could do [self.restartExternal(), restartWindow.destroy()]. This way, it will call restartWindow.destroy() whatever self.restartExternal() returns, whereas how it is, if self.restartExternal() returns False, Python doesn't even check to see if restartWindow.destroy is True or False. Besides that, restartWindow.destroy isn't even called in yours, because you left out the parentheses.
The most basic answer for that would be:
def commend1():
print "hi"
def commend2():
print "hello"
def cmd12():
commend1()
commend2()
#TKinter
button = Button(root, command=cmd12)
You can have your commend1() to call commend2() like this :
def commend1():
print "hi"
commend2()
def commend2():
print "hello"
#TKinter
button = Button(root, command= commend1)