【问题标题】:How to change background/foreground of item in Treeview by tag?如何通过标签更改 Treeview 中项目的背景/前景?
【发布时间】:2019-11-26 15:54:54
【问题描述】:

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

我已经尝试应用样式并将背景设置为所需的 RGB 到 Treeview,但背景仍然是白色。我也尝试设置标签并将标签背景配置为所需的 RGB,但它仍然返回为白色!

    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)

**编辑:

我正在使用 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 时,我没有任何问题(我不确定是哪一个)。

【问题讨论】:

    标签: python tkinter tags treeview styles


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我很确定 'tags' 关键字必须得到一个元组而不是一个字符串,要强制转换,只需写 'minus' 像 ('minus',) 代替。

      编辑:文档实际上说“标签”关键字实际上应该得到一个列表,但我已经看到很多提供元组的示例。我想这是因为可以将元组解析为列表。

      【讨论】:

      • 不,不幸的是@Cesar Cuevas。
      • 好吧,只是为了确定一下,你能不能试着写 'tag' 而不是 'tags',在这种情况下,不要像我之前说的那样给一个字符串而不是一个元组。
      • 已经尝试过tag 而不是tags 当你建议元组时,我也再次尝试了两个标签/s 案例!而且,为了确认,我已经检查过,以防万一我在某个地方覆盖了有关背景的初始说明!
      • 您可以发布整个代码吗?至少只对应于 GUI
      • 我只是想确保没有任何东西影响应用程序的行为,主要是因为我刚刚测试了您的代码并且它有效。
      猜你喜欢
      • 2016-05-08
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多