Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to add a border color to a button in Tkinter?
Next article icon

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:

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:


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp