Tkinter highlightcolor options on Win7 Tkinter highlightcolor options on Win7 tkinter tkinter

Tkinter highlightcolor options on Win7


here is an example for normal buttons:

try:    import Tkinter as tkexcept ImportError:    import tkinter as tkclass 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()

so this adds bindings to react to gaining or losing keyboard focus, you could expand on this to e.g. change the colour of the text also. it should be relatively straightforward to adapt this for checkbuttons.


This is a simple example for what u want

from Tkinter import *from time import sleepfrom random import choiceclass 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)

It's working 100% on Windows 10. so try it on Windows 7I set the color to random so u can define every one with ur "color" to understand what's happened also it's highlightbackground not highlightcolor