you can use the sample way with lambda like this:
button = Button(text="press", command=lambda:[function1(), function2()])
Answer from user15068362 on Stack OverflowHi, 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()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))
python - Have multiple commands when button is pressed - Stack Overflow
python - How do you have 2+ Function Tkinter Button - Stack Overflow
python - Binding one button to two events with Tkinter - Stack Overflow
Tkinter - Same event for multiple buttons - Stack Overflow
You 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))
I don't think you can put two functions to be triggered in a tkinter button; however, you could use a helper function that executes several other functions:
def handle_button_command():
Store_SQLite()
do_also_that()
def Store_SQLite():
pass
def do_also_that():
pass
button = Button(window1, text="Create", command=handle_button_command)
You could also go like:
Button(command=lambda : [some_function(), some_other_function(), some_another_function()])
be mindful that their order do matter. I'd also rather use a handle function like in Reblochon Masque's answer though.
The key is passing add="+" when you bind the handler. This tells the event dispatcher to add this handler to the handler list. Without this parameter, the new handler replaces the handler list.
try:
import Tkinter as tkinter # for Python 2
except ImportError:
import tkinter # for Python 3
def on_click_1(e):
print("First handler fired")
def on_click_2(e):
print("Second handler fired")
tk = tkinter.Tk()
myButton = tkinter.Button(tk, text="Click Me!")
myButton.pack()
# this first add is not required in this example, but it's good form.
myButton.bind("<Button>", on_click_1, add="+")
# this add IS required for on_click_1 to remain in the handler list
myButton.bind("<Button>", on_click_2, add="+")
tk.mainloop()
you could nest both functions inside of another function :) for example:
def addone(num1):
num1=int(num1)+1
def subtractone(num1):
num1=int(num1)-1
def combine():
addone(1)
subtractone(1)
if you wanted to call both of them, you would simply use combine() as the function you call :)
Using a list to reference the dynamically created buttons and lambda to store a reference to the index of the button object. You can determine which button was clicked. In the below examples I use .cget("text") on the button object to demonstrate accessing the button widget.
import tkinter as tk
root = tk.Tk()
root.minsize(200, 200)
btn_list = [] # List to hold the button objects
def on_click(idx):
print(idx) # Print the index value
print(btn_list[idx].cget("text")) #Print the text for the selected button
for i in range(10):
# Lambda command to hold reference to the index matched with range value
b = tk.Button(root, text = f'Button #{i}', command = lambda idx = i: on_click(idx))
b.grid(row = i, column = 0)
btn_list.append(b) # Append the button to a list
root.mainloop()
Alternatively you can use bind and then access the widget from the event object generated.
import tkinter as tk
root = tk.Tk()
root.minsize(200, 200)
def on_click(event):
btn = event.widget # event.widget is the widget that called the event
print(btn.cget("text")) #Print the text for the selected button
for i in range(10):
b = tk.Button(root, text = f'Button #{i}')
b.grid(row = i, column = 0)
# Bind to left click which generates an event object
b.bind("<Button-1>", on_click)
root.mainloop()
- @Steven Summers' first example seems most clear to me, but I think doing it without the list is even clearer.
- The way I understood the question, you not only want to know which button was clicked but you also want each button to call one other, undescribed function (
universalin my example below). In that case, you can use the very handycombine_funcs(see: Have multiple commands when button is pressed) to call two functions from one widget.
Here is my code. Instead of a list, I simply have a string that is changed and printed with each click.
import tkinter as tk
root = tk.Tk()
root.minsize(200, 200)
buttonVal = ''
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def universal():
print 'Universal function is called'
def button_check(buttonName):
buttonVal = buttonName
print buttonVal # Or whatever you want to do with the button info
for i in range(10):
B1 = tk.Button(root, text = 'Button #%s' % i, command = combine_funcs(universal, lambda buttonName = 'Button #%s' % i:button_check(buttonName)))
B1.grid(row = i, column = 0)
root.mainloop()