Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Tkinter - Button that changes its properties on hover
Next article icon

When a user hits the button on the Tkinter Button widget, the command option is activated. In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which you use them.

Method 1: Pass Arguments to Tkinter Button using the lambda function

Import theTkinter package and create a root window. Give the root window a title(using title()) and dimension(using geometry()), now Create a button using (Button()). Use mainloop() to call the endless loop of the window.lambda function creates a temporary simple function to be called when the Button is clicked.

Python3
# importing tkinterimporttkinterastk# defining functiondeffunc(args):print(args)# create root windowroot=tk.Tk()# root window title and dimensionroot.title("Welcome to GeekForGeeks")root.geometry("380x400")# creating buttonbtn=tk.Button(root,text="Press",command=lambda:func("See this worked!"))btn.pack()# running the main looproot.mainloop()

Output:

Pass Arguments to Tkinter Button
using lambda

Method 2: Pass Arguments to Tkinter Button using partial 

Import theTkinter package and create a root window. Give the root window a title(using title()) and dimension(using geometry()), now Create a button using (Button()). Use mainloop() to call the endless loop of the window. command=partial returns a callable object that behaves like a func when it is called.

Python3
# importing necessary librariesfromfunctoolsimportpartialimporttkinterastk# defining functiondeffunction_name(func):print(func)# creating root windowroot=tk.Tk()# root window title and dimensionroot.title("Welcome to GeekForGeeks")root.geometry("380x400")# creating buttonbtn=tk.Button(root,text="Click Me",command=partial(function_name,"Thanks, Geeks for Geeks !!!"))btn.pack()# running the main looproot.mainloop()

Output:

Pass Arguments to Tkinter Button
using partial

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