Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter highlightcolor options on Win7

I'm building a Tkinter GUI (Python 2.7) and running it on a Win7 machine. One of the things I'd like to do is have controls such as buttons & checkboxes take on a different color when they're highlighted (in that the highlight marker itself is a little faint, especially with the default gray background). However, setting highlightcolor to a distinct color (e.g. 'cyan') isn't having any effect on the GUI; the control remains gray whether it has the focus or not. As a cross-check I tried setting all the highlightblah options to something different, but still it shows as gray, with no discernible change in the thickness either.

Is this just a limitation of Tkinter on Win7? Does it just not respond to these options?

like image 278
JDM Avatar asked Feb 04 '16 21:02

JDM


People also ask

Can I use hex colors in Tkinter?

You can use a string specifying the proportion of red, green and blue in hexadecimal digits. For example, "#fff" is white, "#000000" is black, "#000fff000" is pure green, and "#00ffff" is pure cyan (green plus blue). You can also use any locally defined standard color name.

How do I run a Tkinter program in Windows?

The simplest method to install Tkinter in a Windows environment is to download and install ActivePython 3.8 from here. Alternatively, you can create and activate a Conda environment with Python 3.7 or greater that is integrated with the latest version of Tkinter.

What is Tkinter config?

config is used to access an object's attributes after its initialisation. For example, here, you define. l = Label(root, bg="ivory", fg="darkgreen") but then you want to set its text attribute, so you use config : l.


1 Answers

here is an example for normal buttons:

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

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.

like image 173
James Kent Avatar answered Oct 07 '22 14:10

James Kent