Tkinter Highlight Settings on Win7

I create a Tkinter GUI (Python 2.7) and run it on a Win7 machine. One of the things I would like to do is to have controls such as buttons and check boxes highlighted in a different color when they are highlighted (in that the selection marker itself is a little weak, especially with a gray default background). However, setting the highlightcolor to a separate color (for example, 'cyan' ) has no effect on the graphical user interface; the control remains gray, whether it has focus or not. As a cross-validation, I tried to set all highlightblah parameters to something else, but still it appears as gray, with no noticeable change in thickness.

Is this just a limitation of Tkinter on Win7? It just does not respond to these parameters?

+6
source share
2 answers

Here is an example for regular 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 respond to getting or losing keyboard focus, you can expand this, for example. change text color as well. it should be relatively simple to adapt it for check buttons.

0
source

This is a simple example of what you want.

 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) 

It works 100% on Windows 10. so try it on Windows 7 I set the color randomly, so you can define each with ur β€œcolor” to understand what happened and it highlightbackground not highlightcolor

-1
source

Source: https://habr.com/ru/post/1242323/


All Articles