我一直在尝试使用鼠标悬停事件来更改Tkinter中的butten小部件的背景颜色.我在网上得到了一些简单的代码,适用于文本,但颜色不合适.这里是:
from Tkinter import *
root - Tk()
b = Button(root, text='foo')
b.pack()
def enterB(event):
b.configure(text='bar', background='red')
def leaveB(event):
b.configure(text='foo')
b.bind('<Enter>', enterB)
b.bind('<Leave>', leaveB)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
当我将鼠标放在按钮上时,文本会变为"bar",但背景颜色会保持灰色.当我的鼠标离开按钮上方的区域时,背景颜色变为红色,文本变为"foo".这与应该发生的事情相反.
如果我在leaveB函数的b.configure语句中放置background ='blue',并保持其余部分相同,则保留按钮将使按钮保持蓝色,并且它永远不会是红色.
谁能告诉我发生了什么?
谢谢.
首先,我猜这是第2行的拼写错误,应该是root = Tk()
该程序对我来说工作正常,除了从按钮上移除鼠标时背景保持不变的行为red。可以通过稍微修改leaveB函数来改变它,如下所示:
def leaveB(event):
b.configure(text="foo", background="SystemButtonFace")
Run Code Online (Sandbox Code Playgroud)
"SystemButtonFace"如果您使用的是 Windows,默认按钮表面颜色在哪里
| 归档时间: |
|
| 查看次数: |
3589 次 |
| 最近记录: |