2

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

Mr K
  • 111
  • 13

2 Answers2

3

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'))
nda
  • 541
  • 7
  • 18
Mr K
  • 111
  • 13
0

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.

  • No, no luck unfortunately @Cesar Cuevas. – Mr K Jul 17 '19 at 19:25
  • Okay, just to make sure, can you try writing 'tag' instead of 'tags', and in this case, do give a string not a tuple as I said before. – Cesar Cuevas Jul 17 '19 at 19:32
  • Tried already `tag` instead of `tags` and when you suggested tuple, I also tried again both tag/s cases! And also, just to confirm, I've checked just in case have I've overwritten somewhere initial instruction about background! – Mr K Jul 17 '19 at 19:33
  • Is it possible that you post your whole code? at least only what corresponds to GUI – Cesar Cuevas Jul 17 '19 at 19:54
  • I just want to make sure that nothing is affecting the app's behavior, mostly cause I just tested your code and it worked. – Cesar Cuevas Jul 17 '19 at 20:02
  • code is few hundred lines as I'm still working out on optimization (this is my first app in python). GUI part, well, I've attached only two parts that are concerned about treeview customization. There is a frame container, but I've checked that too. No problem to attach it but I was ust concerned about size of it... And to be honest ashamed of Frankencode. :) – Mr K Jul 17 '19 at 21:38