0

I've just rebuilt my linux desktop, now Mint 20.1 Mate. I have python 3.8.5, and Tk 8.6. Unfortunately I don't know for certain what version of Tk I was using last week, I'm sure it's been 8.6 for a long time, but was using Mint 20.1 xcfe with python 3.8.5. Tk was installed via apt install python3-tk

Programs using the Tk GUI, with text widgets like Entry and Text, for the last decade until last week, used to show selected text with a clearly contrasting highlight, just using the default settings. I'm not sure whether it was identical to that used by Mint, but it didn't cause any surprise.

Now I've rebuilt, the selection highlighting is more or less invisible. I can tell it's been highlighted if I look very carefully (a very light grey), but it's now a pain to use. I can make it more visible by altering the background to something contrasting like green or orange, which is not very aesthetically satisfactory.

How can I change Tk's defaults back again? How can I find out what they should be? Does it have user accessible defaults?

Looking over the docs, I can find that there are options to change most things, except for text selection highlight.

It's probably telling me I ought to progress to ttk and styles. But that's a bit to learn, and a lot of programs to change.

Neil_UK
  • 1,043
  • 12
  • 25
  • there is an option to change selected text it should be: `selectforeground` same with `selectbackground` and there is even `selectborderwidth` (at least for Entry widgets) – Matiiss May 05 '21 at 20:19
  • @Matiiss Is there any way to change the defaults of all the widgets? – Neil_UK May 05 '21 at 20:59
  • well there certainly is the option to just go to the tkinter source code and change them manually (not suggested) otherwise I have no idea (except maybe it is that the OS version changed) – Matiiss May 05 '21 at 21:09
  • @Matiiss: there are ways of changing the defaults without modifying the source code. – Bryan Oakley May 05 '21 at 21:12
  • 1
    @Matiiss I've found this, it seems to work `root.tk_setPalette(background='#40E0D0', foreground='black', activeBackground='black', activeForeground=mycolor2, etc ...)` – Neil_UK May 05 '21 at 21:29
  • @Neil_UK: you should write that comment as an answer, that's a great suggestion! – Bryan Oakley May 05 '21 at 21:51

2 Answers2

1

tkinter uses what it calls an option database for its defaults. Different platforms will define this database differently. It could be that your OS changed it, or your specific desktop environment caused it to change.

The text selection is controlled by the selectbackground and selectforeground options. For every option there is a corresponding value in the option database. There are also values for each widget class, so you could set one value for text widgets and a different value for entry widgets.

For selectbackground the name in the option database is selectBackground. You can change the value in the database with the option_set method. Any widgets created after this value has been set will use the new value.

For example, if you want to set the Entry selection background to pink, you could do something like this:

import tkinter as tk

root = tk.Tk()
root.option_add("*Entry.selectBackground", "pink")

Any Entry widget created after that line of code will have pink as the default for the selection background. To change it for all widgets, you can just remove Entry.:

root.option_add("*selectBackground", "pink")

The option database is a bit arcane. It has its roots in the way widget options were specified back when tk only worked on X11-based systems. It's very powerful, but not particularly intuitive.

The most detailed online documentation that I know of is a document titled Options and Tk - A Beginner's Guide. It is written from the perspective of tcl/tk rather than python/tkinter, but the concepts are identical. The Tkinter Life Preserver in the python docs can help with the translation to python.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

After some searching, I came across this command

root.tk_setPalette(background='#40E0D0', foreground='black', etc, ...

It changes all the existing widgets across the application, and sets the defaults for new ones. According to this page it can set the following options ...

foreground / background
activeForeground / activeBackground
selectForeground / selectBackground
highlightColor / highlightBackground
disabledForeground
insertBackground
troughColor
selectColor

Most of those did what I would expect. However insertBackground seems to control the colour of the insert cursor only, and selectColor the background of CheckButtons. The highlightColor and highlightBackground control the outline of widgets with and without focus respectively.

Neil_UK
  • 1,043
  • 12
  • 25