Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Dynamically Resize Buttons When Resizing a Window using Tkinter
Next article icon

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods,tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python withtkinter is the fastest and easiest way to create GUI applications. Creating a GUI usingtkinter is an easy task.

In this article, we are going to create a button that changes its properties on hover.

Step-by-step Approach:

Python3
# import required modulefromtkinterimport*
  • Creating A Universal Function To Give Change On Hover Power To Every Button.

Theconfig() method is used to change the properties of any widget. Note that inconfig() methodbgandbackground are two different options andbackground represents background-color.

Syntax:

widget.config(**options)

Below is the implementation:

Python3
# function to change properties of button on hoverdefchangeOnHover(button,colorOnHover,colorOnLeave):# adjusting background of the widget# background on entering widgetbutton.bind("<Enter>",func=lambdae:button.config(background=colorOnHover))# background color on leving widgetbutton.bind("<Leave>",func=lambdae:button.config(background=colorOnLeave))
  • Create a button in driver code and call the explicit function.
Python3
# Driver Coderoot=Tk()# create button# assign button text along# with background colormyButton=Button(root,text="On Hover - Background Change",bg="yellow")myButton.pack()# call function with background# colors as argumentchangeOnHover(myButton,"red","yellow")root.mainloop()

Below is the complete program based on the above approach:

Python3
# import required modulefromtkinterimport*# function to change properties of button on hoverdefchangeOnHover(button,colorOnHover,colorOnLeave):# adjusting backgroung of the widget# background on entering widgetbutton.bind("<Enter>",func=lambdae:button.config(background=colorOnHover))# background color on leving widgetbutton.bind("<Leave>",func=lambdae:button.config(background=colorOnLeave))# Driver Coderoot=Tk()# create button# assign button text along# with background colormyButton=Button(root,text="On Hover - Background Change",bg="yellow")myButton.pack()# call function with background# colors as argumentchangeOnHover(myButton,"red","yellow")root.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