简体   繁体   中英

How to change background/foreground of item in Treeview by tag?

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.

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

And here is Style:

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

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

**EDIT:

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

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

A solution is found at: https://bugs.python.org/issue36468

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

I'm quite sure that the 'tags' keyword must get a tuple instead of a string, to force the conversion, just write 'minus' like ('minus',) instead.

Edit: Documentation actually says that the 'tags' keyword should actually get a list, but i've seen lots of examples that supply a tuple. I suppose that this is due to the fact that a tuple can be parsed to a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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