python - Win7 上的 Tkinter highlightcolor 选项

标签 python python-2.7 tkinter windows-7

我正在构建一个 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/

相关文章:

python - pandas df 列中的多个值并对另一列值求和

python - 尽管设置了 IAM 权限,但无法从不同的 GCP 项目访问 Google Cloud SQL 实例

python - 在 Python 中 pickle 字典

python - Tkinter 网格间距问题

python - TclError : no display name and no $DISPLAY environment variable in Google Colab

python - 如何在 matplotlib 中制作空白子图?

python - 使用 Pandas 处理 GPS 数据

python-2.7 - PIL - 如何在文本中插入索引或下标?

python - Unix 时间戳转 ISO 8601 时间格式

python - tkinter 标签列表未正确显示