I am trying to change the default background color for selected text in a Tkinter Text widget on Mac OS X when the widget does not have focus. The default unfocused select color is gray. After many hours of searching, I was unable to find an out-of-the-box solution to do this. Here is what I have tried:
selectbackground
option does not change the select color when the widget is not focused. E.g. It stays gray.Text.tag_configure("sel", background=...)
ttk.Style.map
with the "!focus"
state works on Entry widgets (and others), but not Text widgets.So I had to roll my own (see below). Is there a better way to do this?
import Tkinter as tk
# Replace 'tag_out' with 'tag_in'
def replace_tag(widget, tag_out, tag_in):
ranges = widget.tag_ranges(tag_out)
widget.tag_remove(tag_out, ranges[0], ranges[1])
widget.tag_add(tag_in, ranges[0], ranges[1])
def focusin(e):
replace_tag(e.widget, "sel_focusout", "sel")
def focusout(e):
replace_tag(e.widget, "sel", "sel_focusout")
root = tk.Tk()
# Create a Text widget with a red selected text background
text = tk.Text(root, selectbackground="red")
text.pack()
# Add some text, and select it
text.insert("1.0", "Hello, world!")
text.tag_add("sel", "1.0", "end")
# Create a new tag to handle changing the background color on selected text
# when the Text widget loses focus
text.tag_configure("sel_focusout", background="green")
replace_tag(text, "sel", "sel_focusout")
# Bind the events to make this magic happen
text.bind("<FocusIn>", focusin)
text.bind("<FocusOut>", focusout)
# Create an Entry widget to easily test the focus behavior
entry = tk.Entry(root)
entry.pack()
entry.insert("0", "Focus me!")
root.mainloop()
ttk module is used for styling the tkinter widgets such as setting the background color, foreground color, activating the buttons, adding images to labels, justifying the height and width of widgets, etc. In order to add a background color in tkinter widgets, we can specify the background property in the widget.
There are two ways to change the background color of a window in Tkinter: By using the configure(bg=”) method of the tkinter.Tk class. Set the bg property of tkinter.Tk directly.
To manage and give focus to a particular widget, we generally use the focus_set() method. It focuses the widget and makes them active until the termination of the program.
We can use the Tkinter text widget to accept multiline user input. We can insert text, display information, and get the output from the text widget. To highlight the currently selected text in a text widget, we can use the tag_add() method that adds a tag in the current text only.
Digging through the Tk source code lead me to the answer! The inactiveselectbackground
option sets the color.
import Tkinter as tk
root = tk.Tk()
# Create a Text widget with a red selected text background
# And green selected text background when not focused
text = tk.Text(root, selectbackground="red", inactiveselectbackground="green")
text.pack()
# Add some text, and select it
text.insert("1.0", "Hello, world!")
text.tag_add("sel", "1.0", "end")
# Create an Entry widget to easily test the focus behavior
entry = tk.Entry(root)
entry.pack()
entry.insert("0", "Focus me!")
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With