2

I'm making a GUI with a dark theme and would like to save time by setting the default parameters using an option database but only some of the options are working. Options like "background" and "font" are working correctly but other things like changing the activebackground or selectcolor for a Radiobutton do NOT work. Changing the "insertbackground" on an entry widget also doesn't work.

I'm not sure why some options are working and others are not. If I pass in the same parameters when creating the widget it does work. eg:

myRadio = Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)

I tried this two ways.

import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas
mw  = Tk()

mw.option_readfile('StyleDatabase.txt')

# code here...

mw.mainloop()

The "StyleDatabase.txt" file contains the following:

*background: #000008
*foreground: grey90
*font: '', 11
*Entry*background: #404050
*Radiobutton*selectcolor: #FF0000  <---- This one doesn't work.

# also tried:
*selectcolor: #FF0000
*Radiobutton.selectcolor: #FF0000 <---- Neither worked

I also tried using the mw.option_add() function, none of the following worked:

mw.option_add('*selectbackground', 'blue')
mw.option_add('*Radiobutton*selectbackground', 'blue')
mw.option_add('*Radiobutton.selectbackground', 'blue')

As far as I know there aren't any other ways to do this.

EDIT: Here's a workable script to demonstrate the issue, Python 3.x

import tkinter
from tkinter import Tk, ttk, Frame, Entry, Label, Button, Toplevel, BooleanVar, IntVar, Radiobutton, StringVar, Canvas

class MainUI:
    def __init__(self, master):
        self.master = master 

        Label(self.master, text='Hello').pack(side='top')

        Entry(self.master).pack(side='top')
        Entry(self.master, insertbackground='red').pack(side='top')

        Button(self.master, text='Cyan Button').pack(side='top')

mw = Tk()

mw.option_add('*background', 'blue') # works
mw.option_add('*foreground', 'white') # works
mw.option_add('*Button.foreground', 'cyan') # works

mw.option_add('*insertbackground', 'red') # does not work. Works if i pass the argument in when creating the widget.
mw.option_add('*Entry.insertbackground', 'red') # also does not work
mw.option_add('*Entry*insertbackground', 'red') # also does not work

mainUI = MainUI(mw)

mw.mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
Dave1551
  • 323
  • 2
  • 13

1 Answers1

2

Question: Option database only partially working

You are stuck with the different option names and aliases use by tkinter and the underlying Tcl.


Your option parameter have to be, note the upercase Color:

*Radiobutton*selectColor: #FF0000

You can get the name used by .option_readfile or .option_add from .config:

myRadio = tk.Radiobutton(frame, selectcolor='#FF0000', **otherKwargs)
myRadio.grid()

print('{}'.format(myRadio.config()['selectcolor']))

>>> ('selectcolor', 'selectColor', 'Background', '#ffffff', '#FF0000')

This answer, What are these strange options returned by the method configure?, explains each option of the five values.

The Second name, here 'selectColor', are used by the option database.


Note: Options not shown from <widget instance>.config() can not be set.

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

stovfl
  • 14,998
  • 7
  • 24
  • 51