鼠标悬停时更改 Tkinter 中按钮的颜色
假设我们要创建一个应用程序,其中希望在鼠标悬停时更改按钮小部件的颜色。可以通过定义事件回调来实现悬停属性。
要在鼠标悬停时更改按钮的颜色,我们需要绑定**<Enter>**和**<Leave>**事件。对于每个事件,我们将配置按钮属性,例如背景色、前景色等。
示例
#Import required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("750x250")
#Define functions
def on_enter(e):
button.config(background='OrangeRed3', foreground= "white")
def on_leave(e):
button.config(background= 'SystemButtonFace', foreground= 'black')
#Create a Button
button= Button(win, text= "Click Me", font= ('Helvetica 13 bold'))
button.pack(pady= 20)
#Bind the Enter and Leave Events to the Button
button.bind('<Enter>', on_enter)
button.bind('<Leave>', on_leave)
win.mainloop()输出
运行以上代码会显示一个包含按钮的窗口。
现在,将鼠标悬停在显示窗口中的按钮上。它将更改其基本颜色,例如背景色和前景色。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP