我正在构建一个 Tkinter GUI (Python 2.7) 并在 Win7 机器上运行它。我想做的一件事是让按钮和复选框等控件在突出显示时呈现不同的颜色(因为突出显示标记本身有点微弱,尤其是在默认的灰色背景下)。但是,设置 highlightcolor
到不同的颜色(例如 'cyan'
)对 GUI 没有任何影响;无论是否有焦点,控件都保持灰色。作为交叉检查,我尝试设置所有 highlightblah
选择不同的东西,但它仍然显示为灰色,厚度也没有明显的变化。
这只是 Tkinter 在 Win7 上的限制吗?它只是不响应这些选项吗?
最佳答案
这是普通按钮的示例:
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
class HighlightButton(tk.Button):
def __init__(self, master, *args, **kwargs):
tk.Button.__init__(self, master, *args, **kwargs)
# keep a record of the original background colour
self._bg = self['bg']
# bind to focus events
self.bind('<FocusIn>', self._on_focus)
self.bind('<FocusOut>', self._on_lose_focus)
def _on_focus(self, event):
self.configure(bg=self['highlightcolor'])
def _on_lose_focus(self, event):
self.configure(bg=self._bg)
root = tk.Tk()
hb = HighlightButton(root, text='Highlight Button', highlightcolor='cyan')
hb.pack()
t = tk.Text(root)
t.pack()
root.mainloop()
因此这会添加绑定(bind)以对获得或失去键盘焦点使用react,您可以将其扩展到例如也改变文本的颜色。将其用于检查按钮应该相对简单。
关于python - Win7 上的 Tkinter highlightcolor 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35211601/