如何通过标签更改 Treeview 中项目的背景/前景? [英] How to change background/foreground of item in Treeview by tag?

查看:31
本文介绍了如何通过标签更改 Treeview 中项目的背景/前景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Treeview 中为标签应用不同的背景,但是当我将标签设置为减号"并尝试将标签配置为具有黑色背景时,它仍然返回白色背景.

I want to apply different background for tags in Treeview but when I set tag for example as "minus" and try to configure tag to have black background it stills returns white background.

我尝试应用样式并将背景设置为所需的 RGB 到 Treeview,但背景保持白色.我还尝试设置标签并将标签背景配置为所需的 RGB,但它仍然返回为白色!

I've tried applying Style and set background to desired RGB to Treeview but background remains white. I've also tried to set tags and configure tag background to desired RGB but it is still returned as white!

    for row in rows:
        self.treeplan.insert('', 'end', text=str(cpt),
                             values=(row[1], row[2], row[3], row[4], 
                                     row[5], row[6], row[7], row[8], 
                                     row[9], row[10], row[11], row[12], 
                                     row[13], row[14], row[15]), 
                             tags='minus')
        cpt += 1
    self.treeplan.tag_configure('minus', background="#%02x%02x%02x" % (61, 72, 73), 
                                foreground="red")

这里是样式:

    self.style = ttk.Style(master)
    self.style.theme_use("clam")
    self.style.configure("mystyle.Treeview", bd=0, background="black", 
                          foreground="white", fieldbackground="red")
    self.style.configure("mystyle.Treeview.Heading", font=('Calibri', 9,
                          'bold'), background="#383838", foreground="white")
    self.style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', 
                      {'sticky': 'news'})])

我实际上想将所有 Treeview 项目的背景设置为 "#%02x%02x%02x" % (61, 72, 73)

I actually want to set background of all Treeview items to "#%02x%02x%02x" % (61, 72, 73)

**

我正在使用 Treeview 添加部分代码:

I'm adding part of code with Treeview:

self.treeplan_frame = Frame(master, background=rgbcon2((39, 46, 46)))
self.treeplan_frame.grid(row=7, column=0, columnspan=8, sticky="nws", pady=10, padx=10)
self.treeplan = ttk.Treeview(self.treeplan_frame, height=19, style="mystyle.Treeview")

如您所见,我尝试使用 Style 更改背景,但没有成功.然后我尝试通过配置标签(标签/标签)来改变.我检查了不同的线程,我不太清楚为什么在这种情况下它不起作用.顺便提一句.我是 Python 3.7 和 Tkinter 8.6.当我有 3.6 时,我没有任何问题,而且以前版本的 Tkinter(我不确定是哪个).

As you see, I tried to change background with Style with no luck. Then I've tried to change by configuring tags (tag/tags). I've checked different threads and I'm not quite clear why in this case it doesn't work. Btw. I it is Python 3.7 and Tkinter 8.6. When I had 3.6 I hadn't any problems and previous version of Tkinter (I'm not sure which one).

推荐答案

解决方案位于:https://bugs.python.org/issue36468

根据链接,问题出在 Tkinter 版本上.人们认为问题出在 Python 版本上,但那是因为 Python 版本使用了有问题的 Tkinter 版本.

According to the link, the issue is with the Tkinter version. People are thinking the issue is with the Python version but that is because the Python version uses the faulty Tkinter version.

def fixed_map(option):
# Fix for setting text colour for Tkinter 8.6.9
# From: https://core.tcl.tk/tk/info/509cafafae
#
# Returns the style map for 'option' with any styles starting with
# ('!disabled', '!selected', ...) filtered out.

# style.map() returns an empty list for missing options, so this
# should be future-safe.
return [elm for elm in style.map('Treeview', query_opt=option) if
  elm[:2] != ('!disabled', '!selected')]

style = ttk.Style()
style.map('Treeview', foreground=fixed_map('foreground'),
       background=fixed_map('background'))

这篇关于如何通过标签更改 Treeview 中项目的背景/前景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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