Tkinter ttk: background/foregound color will not work on my computer

我只是一个虾纸丫 提交于 2021-01-29 13:10:46

问题


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('',0,values=['red', 'blue'], tags= ('even',))
y['columns'] = ('color1','color2')
for item in y['columns']:
    y.heading(item, text=item)
y.tag_configure('even',foreground='yellow',font=('',25))
y.pack()
x.mainloop()

It changes the font but not the background color. This code does work when run from https://repl.it/languages/tkinter and another user pointed out he had success running it from jupyter notebook. The tkinter/tcl versions are identical to the ones on both my computers. But still, I get plain default settings.

This also appears to be consistent across all ttk widgets, such as the combo boxes.

I have tried every theme and messed around with the mapping in the tcl code. Very puzzled as to why I am running into this issue. Has anyone here encountered this? Might be time to switch to pyQT.


回答1:


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.




回答2:


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.)




回答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.



来源:https://stackoverflow.com/questions/56616292/tkinter-ttk-background-foregound-color-will-not-work-on-my-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!