Skip to content Skip to sidebar Skip to footer

Tkinter Ttk: Background/foregound Color Will Not Work On My Computer

If I run this code via IDLE or a virtual environment in pycharm on both windows 10 and 7: import tkinter as tk from tkinter import ttk x = tk.Tk() y = ttk.Treeview(x) y.insert('',

Solution 1:

The default theme on Windows when running natively (which might be winnative, xpnative or vista) for most ttk widgets (especially including the treeview) doesn't let you change the background colour. Other themes (e.g., alt, classic or clam) let you change that aspect (or rather they don't ignore it); it's up to the theme to choose whether or not to ignore your setting, and native themes prioritise following the platform GUI design guidelines over the directives you provide.

See also this question: How do I change the overall theme of a tkinter application?

Note that other platforms may have even more restrictive themes; the aqua theme on OSX is particularly tightly defined. (Changing theme is not enough to make an application feel native though; different platforms also prefer different widgets for some operations and have different ways of laying out their GUIs. Also some aspects of GUIs just work totally differently. Cross platform GUI creation remains difficult.)

Solution 2:

First of all, you should state what OS your computer is running. Second of all, a lot of Python GUI frameworks don't fully work on Mac OS (in-case that is your OS type). For example, for many Python GUI frameworks/toolkits, Mac OS has a tendency to block the ability of the GUI to have a non-standard background color for the open windows. I know that most Python GUIs work flawlessly on Windows OS, but I'm unsure of how they work on Linux, but I'm pretty sure they have similar issues to Mac OS since they're both unix based (I could be wrong here, but I don't remember off of the top of my head, so correct me if I'm wrong). Try looking up the docs for tkinter and seeing what notices they have for your particular OS.

Also, I notice that you're wanting to change the background color, but I only see a foreground tag. The foreground tag you have is simply changing the font color, but if you change it to background, it does change the background to yellow.

Solution 3:

A user on a previous question posted this link before he deleted his answer: https://core.tcl-lang.org/tk/tktview/509cafafae48cba46796e12d0503a335f0dcfe0b

Which led me in the right direction. The fix is to delete some code from the tcl theme source code. Which is found in pythons folder under tcl/ttk. Open the trouble theme(ex.clam, winnative), and find this bit of code:

ttk::style map Treeview \
        -background [list disabled $colors(-frame)\
                {!disabled !selected} $colors(-window) \
                selected $colors(-selectbg)] \
        -foreground [list disabled $colors(-disabledfg) \
                {!disabled !selected} black \
                selected $colors(-selectfg)]

the {!disabled !selected} $colors(-window) \ and {!disabled !selected} black \ need to be deleted. cjmcdonald discovered this on the tcl-lang forum. You should end up with:

ttk::style configure Treeview -background $colors(-window)
    ttk::style map Treeview \
        -background [list disabled $colors(-frame)\
                selected $colors(-selectbg)] \
        -foreground [list disabled $colors(-disabledfg) \
                selected $colors(-selectfg)]

The only way I have been able to get this to work is to delete straight from the sourcecode. I am sure someone here can streamline this into python.

This is only a fix for the Treeview widget and not the others.

Post a Comment for "Tkinter Ttk: Background/foregound Color Will Not Work On My Computer"