简体   繁体   English

Tkinter ttk:背景/前景色在我的计算机上不起作用

[英]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: 如果我通过Windows 10和7上的pycharm中的IDLE或虚拟环境运行此代码:

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. https://repl.it/languages/tkinter运行时,此代码可以正常工作,另一个用户指出他已成功从jupyter笔记本运行它。 The tkinter/tcl versions are identical to the ones on both my computers. tkinter / tcl版本与两台计算机上的相同。 But still, I get plain default settings. 但是,我仍然得到普通的默认设置。

This also appears to be consistent across all ttk widgets, such as the combo boxes. 在所有ttk小部件(例如组合框)中,这似乎也是一致的。

I have tried every theme and messed around with the mapping in the tcl code. 我尝试了所有主题,并弄乱了tcl代码中的映射。 Very puzzled as to why I am running into this issue. 为什么我遇到这个问题感到非常困惑。 Has anyone here encountered this? 这里有人遇到过吗? Might be time to switch to pyQT. 可能是时候切换到pyQT了。

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). 第二,许多Python GUI框架不能在Mac OS上完全运行(如果您的OS类型)。 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. 例如,对于许多Python GUI框架/工具包,Mac OS倾向于阻止GUI为打开的窗口提供非标准背景色的功能。 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). 我知道大多数Python GUI在Windows操作系统上都可以完美运行,但是我不确定它们在Linux上如何工作,但是我很确定它们与Mac OS有类似的问题,因为它们都是基于Unix的(我在这里可能是错误的) ,但我不记得我的头顶了,如果我错了,请纠正我)。 Try looking up the docs for tkinter and seeing what notices they have for your particular OS. 尝试查找有关tkinter的文档,并查看它们对您的特定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. 您拥有的前台标签只是在更改字体颜色,但是如果将其更改为背景,则会将背景更改为黄色。

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. 对于大多数ttk小部件(尤其是包括treeview),在本机运行时(可能是winnativexpnativevista ),Windows上的默认主题不允许您更改背景颜色。 Other themes (eg, alt , classic or clam ) let you change that aspect (or rather they don't ignore it); 其他主题(例如altclassicclam )可让您更改该方面(或者说他们不会忽略它); 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. 由主题决定是否忽略您的设置,并且本机主题优先于平台GUI设计准则而不是您提供的指令。

See also this question: How do I change the overall theme of a tkinter application? 另请参阅以下问题: 如何更改tkinter应用程序的整体主题?

Note that other platforms may have even more restrictive themes; 请注意,其他平台可能具有更多限制性主题。 the aqua theme on OSX is particularly tightly defined. OSX上的aqua主题特别严格地定义。 (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.) (但是,不断变化的主题不足以使应用程序看起来像是本机;不同的平台还对某些操作更喜欢使用不同的小部件,并且具有不同的GUI布局方式。GUI的某些方面的工作方式完全不同。跨平台的GUI创建仍然很困难。 )

A user on a previous question posted this link before he deleted his answer: https://core.tcl-lang.org/tk/tktview/509cafafae48cba46796e12d0503a335f0dcfe0b 上一个问题的用户在删除答案之前发布了此链接: 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. 解决方法是从tcl主题源代码中删除一些代码。 Which is found in pythons folder under tcl/ttk. 可以在tcl / ttk下的pythons文件夹中找到。 Open the trouble theme(ex.clam, winnative), and find this bit of code: 打开麻烦主题(例如clam,winnative),然后找到以下代码:

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. {!disabled !selected} $colors(-window) \\{!disabled !selected} black \\需要删除。 cjmcdonald discovered this on the tcl-lang forum. cjmcdonald在tcl-lang论坛上发现了这一点。 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. 我确信这里有人可以将其简化为python。

This is only a fix for the Treeview widget and not the others. 这仅是针对Treeview小部件的修复,而不是其他修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM