Tkinter ttk:背景/前景色在我的电脑上不起作用 [英] Tkinter ttk: background/foregound color will not work on my computer

查看:15
本文介绍了Tkinter ttk:背景/前景色在我的电脑上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Windows 10 和 7 上通过 IDLE 或 pycharm 中的虚拟环境运行此代码:

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

它改变了字体而不是背景颜色.当从 https://repl.it/languages/tkinter 和另一个用户指出时,此代码确实有效他成功地从 jupyter notebook 运行它.tkinter/tcl 版本与我两台计算机上的版本相同.但是,我还是得到了简单的默认设置.

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.

这在所有 ttk 小部件中似乎也是一致的,例如组合框.

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

我尝试了所有主题,并在 tcl 代码中搞砸了映射.很困惑为什么我会遇到这个问题.这里有人遇到过吗?可能是时候切换到 pyQT 了.

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.

推荐答案

上一个问题的用户在删除他的答案之前发布了此链接:https://core.tcl-lang.org/tk/tktview/509cafafae48cba46796e12d0503a335f0dcfe0b">

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

这让我朝着正确的方向前进.修复方法是从 tcl 主题源代码中删除一些代码.可以在 tcl/ttk 下的 pythons 文件夹中找到.打开麻烦主题(ex.clam, winnative),找到这段代码:

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

{!disabled !selected} $colors(-window) \{!disabled !selected} black \ 需要删除.cjmcdonald 在 tcl-lang 论坛上发现了这一点.你应该得到:

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

我能够让它工作的唯一方法是直接从源代码中删除.我相信这里有人可以将其简化为 python.

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.

这只是对 Treeview 小部件的修复,而不是其他小部件.

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

这篇关于Tkinter ttk:背景/前景色在我的电脑上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆