Win7上的Tkinter highlightcolor选项

时间:2016-02-04 21:04:51

标签: python-2.7 tkinter windows-7

我正在构建一个Tkinter GUI(Python 2.7)并在Win7机器上运行它。我想做的其中一件事就是有按钮和按钮等控件。复选框在突出显示时呈现不同的颜色(突出显示标记本身有点模糊,特别是默认的灰色背景)。但是,将highlightcolor设置为不同的颜色(例如'cyan')对GUI没有任何影响;无论是否有焦点,控件都保持灰色。作为交叉检查,我尝试将所有highlightblah选项设置为不同的选项,但仍显示为灰色,厚度也没有明显变化。

这仅仅是Win7上Tkinter的限制吗?它只是不回应这些选项吗?

2 个答案:

答案 0 :(得分:0)

这是普通按钮的示例:

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()

因此这会增加绑定以对获得或失去键盘焦点做出反应,您可以对此进行扩展,例如也更改文本的颜色。使其适应复选框应该相对简单。

答案 1 :(得分:-1)

这是你想要的一个简单的例子

from Tkinter import *
from time import sleep
from random import choice


class TestColor():

    def __init__(self):
        self.root = Tk()
        self.button = Button(self.root, text = "buggton", command = self.ChangeColor, bg = "green", fg = "Black", activebackground = "Red", highlightbackground="Black")
        self.button.grid(row=0, column=0)
        self.RanDomiZeColor = ["blue", "black", "white", "yellow"]
        self.root.mainloop()

    def ChangeColor(self):
        self.button = Button(self.root, text = "buggton", command = self.ChangeColor, bg = choice(self.RanDomiZeColor), fg = choice(self.RanDomiZeColor), activebackground = choice(self.RanDomiZeColor), highlightbackground = choice(self.RanDomiZeColor))
        self.button.grid(row=0, column=0)

try: TestColor()
except Exception as why: print why; sleep(10)

它在Windows 10上100%工作,所以在Windows 7上试试吧 我将颜色设置为随机,因此您可以使用ur&#34; color&#34;来定义每个颜色。了解发生的事情highlightbackground而不是highlightcolor