Tkinter: Query system preferences colors

Jeff Epler jepler at unpythonic.net
Mon Aug 26 17:12:00 EDT 2002


This data appears in the X Resource database, accessed through the
"option" command in tcl.  This appears to be reflected in the
option_{add,clear,get,readfile} widget methods in Tkinter.  It's put
there by selecting an option such as "apply themes to legacy
applications" in your desktop environment's configuration.

When executing the following code, both the selected item in the listbox
and the rectangle in the canvas are given the same color:
    >>> t = Tk()
    >>> t.option_add("*selectBackground", "orange")
    >>> l = Listbox(t)
    >>> l.pack()
    >>> l.insert("end", "1")
    >>> l.insert("end", "2")
    >>> c = Canvas(t)
    >>> c.pack()
    >>> i = c.create_rectangle((10, 10, 40, 40), fill=c.option_get("selectBackground", "Foreground"))
If the option is *not* given, then .option_get() returns "" and there is
no fill for the canvas, so it's still not quite what you want.  A
construct like
    fill = c.option_get("selectBackground", "Foreground") or "#c3c3c3"
can help with this.  So can adding your options at the widgetDefault
priority, maybe.

I don't know of a good document that describes how to use the option
database, unfortunately.

Jeff




More information about the Python-list mailing list