The button widget in Tkinter provides a way to interact with the application. The user presses a button to perform certain actions that are attached to that button. In general, we user provides a single action to a button but what if the user wants to attach more than one action to a button.
In this article, we are going to see how we can bind more than one action/command to a single button.
To create a button in Tkinter please follow the below syntax.
Syntax: Button(master, text=”Button”, command=function, options, …)
Parameters:
- master: refers to the top-level window in which button is placed
- text: Text to show button
- command: An action which will be called on button press
There are other options as well but they are rarely used.- compound: To show both image and text
- image: To show image
- pady: To provide vertical padding
- padx: To provide horizontal padding
Method 1: By using the lambda function and list
In this method, we are going to pass a list of functions to lambda, and then that lambda will be passed to command.
Python3
# Import tkinter and Button Widget from tkinter import Tk from tkinter.ttk import Button # Demo function 1 def fun1(): print ( "Function 1" ) # Demo function 2 def fun2(): print ( "Function 2" ) if __name__ = = "__main__" : # Creating top-level window master = Tk() # Setting window title master.title( "Bind multiple function to Button" ) # Setting window Dimensions master.geometry( "400x250" ) # Creating a button with more than one command using lambda button = Button(master, text = "Button" , command = lambda : [fun1(), fun2()]) # Attaching button to the top-level window # Always remember to attach your widgets to the top-level button.pack() # Mainloop that will run forever master.mainloop() |
Output:
Method 2: By creating our own generic function that will call functions for us.
Python3
# Import tkinter and Button Widget from tkinter import Tk from tkinter.ttk import Button # funcs parameter will have the reference # of all the functions that are passed as arguments i.e "fun1" and "fun2" def combine_funcs( * funcs): # this function will call the passed functions # with the arguments that are passed to the functions def inner_combined_func( * args, * * kwargs): for f in funcs: # Calling functions with arguments, if any f( * args, * * kwargs) # returning the reference of inner_combined_func # this reference will have the called result of all # the functions that are passed to the combined_funcs return inner_combined_func # Demo function 1 def fun1(): print ( "Function 1" ) # Demo function 2 def fun2(): print ( "Function 2" ) if __name__ = = "__main__" : # Creating top-level window master = Tk() # Setting window title master.title( "Bind multiple function to Button" ) # Setting window Dimensions master.geometry( "400x250" ) # Creating a button with more than one # command our own generic function button = Button(master, text = "Button" , command = combine_funcs(fun1, fun2)) # Attaching button to the top-level window # Always remember to attach your widgets to the top-level button.pack() # Mainloop that will run forever master.mainloop() |
In the above method, you may be wondering how we are going to pass arguments to fun1 and fun2 because if we do the following
combine_funcs(fun1(arguments), fun2(arguments))
It will immediately call the functions as soon as the application runs, but we want that these functions should be called only when the button is pressed. So the answer is simple if you want to pass arguments to fun1 or fun2 use the below syntax:
combine_funcs(lambda: fun1(arguments), lambda: fun2(arguments))
Let see the below example where we actually have parameters to fun1 and fun2.
Python3
# Import tkinter and Button Widget from tkinter import Tk from tkinter.ttk import Button # funcs parameter will have the reference # of all the functions that are # passed as arguments i.e "fun1" and "fun2" def combine_funcs( * funcs): # this function will call the passed functions # with the arguments that are passed to the functions def inner_combined_func( * args, * * kwargs): for f in funcs: # Calling functions with arguments, if any f( * args, * * kwargs) # returning the reference of inner_combined_func # this reference will have the called result of all # the functions that are passed to the combined_funcs return inner_combined_func # Demo function 1 with params def fun1(param): print ( "Function 1 {}" . format (param)) # Demo function 2 with params def fun2(param): print ( "Function 2 {}" . format (param)) if __name__ = = "__main__" : # Creating top-level window master = Tk() # Setting window title master.title( "Bind multiple function to Button" ) # Setting window Dimensions master.geometry( "400x250" ) # Creating a button with more than # one command our own generic function button = Button(master, text = "Button" , # Passing arguments to "fun1" and "fun2" command = combine_funcs( lambda : fun1( "Function 1 PARAM" ), lambda : fun2( "Function 2 PARAM" ))) # Attaching button to the top-level window # Always remember to attach your widgets to the top-level button.pack() # Mainloop that will run forever master.mainloop() |
Output: