Reputation: 690
I have a Text widget where I display some data. I insert text with alternating colors, to better be able to separate the inserted lines. However, now when I select text within the Text widget, the background color of the selected text is not visible any longer. The default is that selected text has a light gray background with black text, but in my case only the black text is shown.
I made the custom background color in this way:
self.txt = tk.Text(self.midframe, wrap=tk.NONE, yscrollcommand=yscrollbar.set, fg="white", bg="black")
self.txt.tag_configure("even_line", background="#13001a")
self.txt.tag_configure("odd_line", background="#001a00")
...
self.txt.insert(tk.END, header, "odd_line")
for i, line in enumerate(history):
if i % 2 == 0:
self.txt.insert(tk.END, line, "even_line")
else:
self.txt.insert(tk.END, line, "odd_line")
See also what is happening in the small video link below. In the first case all the lines in my text has custom background. When I press "Job History", then only the odd lines have a custom background.
Here you see that the text appears to disappear when I select it.
In this case, where the even line background tag has been disabled, we can see the selected text on half of the lines.
So how can I fix this?
Upvotes: 0
Views: 463
Reputation: 385980
Since any given range of text can have more than one tag, tkinter needs a way to know which tag takes precedence. It does this by assigning a priority order. By default, tags created later have a higher priority over tags created earlier.
The selection is represented by a range of text with the "sel" tag. In order for this tag to have precedence over your custom tags, you need to raise it's priority to be higher than the tags you created. You can adjust this priority with the tag_raise
and tag_lower
methods.
For example, to make sure that the "sel" tag has the highest priority, you can do this after creating all of your other tags:
self.txt.tag_raise("sel")
Upvotes: 1