Python 将颜色绑定到Tkinter中的按钮会导致错误
我目前正在尝试在程序中实现代码,以便在用户将鼠标光标悬停在按钮上时更新按钮颜色。程序识别悬停,但返回一个错误Python 将颜色绑定到Tkinter中的按钮会导致错误,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我目前正在尝试在程序中实现代码,以便在用户将鼠标光标悬停在按钮上时更新按钮颜色。程序识别悬停,但返回一个错误 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__ return self.func(*args) File "C:\Users\oiest\Documents\
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\oiest\Documents\Programs\iNTMI\v1.1.3b\iNTMI.py", line 252, in <lambda>
achievementsButton.bind("<Enter>", lambda event: achievementsButton.configure(bg = "red"))
File "C:\Python34\lib\tkinter\__init__.py", line 1270, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1261, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
ttk.Button
实例没有bg
或background
属性。有两种解决方案:
- 使用普通的
,该按钮具有bg属性tkinter.按钮
- 继续使用ttk.按钮,并使用样式对象对其进行配置。有关更多信息,请参阅。例如:
从Tkinter导入*
导入ttk
root=Tk()
s=ttk.Style()
s、 配置(“常规.TButton”,background=“red”)
s、 配置(“onhover.TButton”,background=“white”)
按钮=ttk.button(根,style=“regular.TButton”)
button.pack()
button.bind(“,lambda事件:button.configure(style=“onhover.TButton”))
button.bind(“,lambda事件:button.configure(style=“regular.TButton”))
root.mainloop()
但是,这只会更改实际按钮后面区域的背景色,而不是按钮的面。
post似乎表明不可能更改ttk按钮的面部颜色。所以没有实际的方法使用ttk保持Windows 7/8/10的漂亮按钮设计?这有点令人失望,但你确实帮了我。非常感谢。
achievementsButton.bind("<Enter>", lambda event: achievementsButton.configure(bg = "red"))
achievementsButton.bind("<Leave>", lambda event: achievementsButton.configure(bg = "white"))
achievementsButton = ttk.Button(self, text = "Achievements", command = lambda: controller.show_frame(achievements), width = "25")
from Tkinter import *
import ttk
root = Tk()
s = ttk.Style()
s.configure("regular.TButton", background="red")
s.configure("onhover.TButton", background="white")
button = ttk.Button(root, style="regular.TButton")
button.pack()
button.bind("<Enter>", lambda event: button.configure(style="onhover.TButton"))
button.bind("<Leave>", lambda event: button.configure(style="regular.TButton"))
root.mainloop()