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 thelambda 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 WidgetfromtkinterimportTkfromtkinter.ttkimportButton# Demo function 1deffun1():print("Function 1")# Demo function 2deffun2():print("Function 2")if__name__=="__main__":# Creating top-level windowmaster=Tk()# Setting window titlemaster.title("Bind multiple function to Button")# Setting window Dimensionsmaster.geometry("400x250")# Creating a button with more than one command using lambdabutton=Button(master,text="Button",command=lambda:[fun1(),fun2()])# Attaching button to the top-level window# Always remember to attach your widgets to the top-levelbutton.pack()# Mainloop that will run forevermaster.mainloop()
Output:
Method 2: By creating our own generic function that will call functions for us.
Python3# Import tkinter and Button WidgetfromtkinterimportTkfromtkinter.ttkimportButton# funcs parameter will have the reference# of all the functions that are passed as arguments i.e "fun1" and "fun2"defcombine_funcs(*funcs):# this function will call the passed functions# with the arguments that are passed to the functionsdefinner_combined_func(*args,**kwargs):forfinfuncs:# Calling functions with arguments, if anyf(*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_funcsreturninner_combined_func# Demo function 1deffun1():print("Function 1")# Demo function 2deffun2():print("Function 2")if__name__=="__main__":# Creating top-level windowmaster=Tk()# Setting window titlemaster.title("Bind multiple function to Button")# Setting window Dimensionsmaster.geometry("400x250")# Creating a button with more than one# command our own generic functionbutton=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-levelbutton.pack()# Mainloop that will run forevermaster.mainloop()
In the above method, you may be wondering how we are going to pass arguments tofun1 andfun2because 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 tofun1 andfun2.
Python3# Import tkinter and Button WidgetfromtkinterimportTkfromtkinter.ttkimportButton# funcs parameter will have the reference# of all the functions that are# passed as arguments i.e "fun1" and "fun2"defcombine_funcs(*funcs):# this function will call the passed functions# with the arguments that are passed to the functionsdefinner_combined_func(*args,**kwargs):forfinfuncs:# Calling functions with arguments, if anyf(*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_funcsreturninner_combined_func# Demo function 1 with paramsdeffun1(param):print("Function 1{}".format(param))# Demo function 2 with paramsdeffun2(param):print("Function 2{}".format(param))if__name__=="__main__":# Creating top-level windowmaster=Tk()# Setting window titlemaster.title("Bind multiple function to Button")# Setting window Dimensionsmaster.geometry("400x250")# Creating a button with more than# one command our own generic functionbutton=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-levelbutton.pack()# Mainloop that will run forevermaster.mainloop()
Output: