[英]How do I change background colour of a button on hover in Tkinter?
I want to change my background and foreground colour of a button on mouse cursor hover in the Python Tkinter module.我想更改鼠标 cursor hover 中 Python Z6008BBEA5A8F51889 中的按钮的背景和前景色。 I am able to change the background colour of the button once before packing it to the main window.
在将按钮打包到主 window 之前,我可以更改按钮的背景颜色一次。 But after the
window.mainloop()
line I cannot execute anymore line until the main window destroyed (or closed).但是在
window.mainloop()
行之后,我无法再执行行,直到主 window 被破坏(或关闭)。
I am asking that is there any way to change the button colour (background and foreground) on mouse hover even after the window.mainloop()
line?我问即使在
window.mainloop()
行之后,是否有任何方法可以更改鼠标 hover 上的按钮颜色(背景和前景)?
My code我的代码
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Test", fg='#03045e', command=terminate_instant,
relief=tkinter.RIDGE, bg='#caf0f8', activebackground='#ef233c',
activeforeground='white')
button.pack(side=tkinter.BOTTOM)
window.mainloop()
You can use <Enter>
and <Leave>
events to change the fg
and bg
color of the button:您可以使用
<Enter>
和<Leave>
事件来更改按钮的fg
和bg
颜色:
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Test", fg='#03045e', command=terminate_instant,
relief=tkinter.RIDGE, bg='#caf0f8', activebackground='#ef233c',
activeforeground='white')
button.pack(side=tkinter.BOTTOM)
button.bind("<Enter>", lambda e: button.config(fg='#caf0f8', bg='#03045e'))
button.bind("<Leave>", lambda e: button.config(fg='#03045e', bg='#caf0f8'))
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.