Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide ttk Treeitem indicators in a Python GUI

I'm building a Python GUI application using ttk treeviews. On Linux, when a Treeitem has child items, it displays an arrow to show that the row can be expanded. I want to hide this indicator arrow (I am using other ways to hint that the row can be expanded). How can I do this?

If I run Style().element_names() I see that there's a Treeview element and a Treeitem.indicator element. If I run Style().configure("Treeview", padding=50), I see the padding style get applied when I create the treeview, so I feel confident that any style I correctly apply to Treeitem.indicator should be visible also.

Running Style().element_options("Treeitem.indicator"), I see ('-foreground', '-indicatorsize', '-indicatormargins'). Running Style().lookup("Treeitem.indicator", "foreground") gives me "#000000", so it appears that value is initialized. If I try Style().configure("Treeview", foreground="#123456") I don't see the indicator arrow change color, though running Style.lookup("Treeitem.indicator", "foreground") shows me "#123456" as expected. My plan was to set the indicatorsize to 0 to make the arrow go away entirely, but I cannot even successfully edit the color. What am I doing wrong here and is there a better way to hide the indicator? In case it matters, I'm running Python 3.5.0.

like image 224
Zeke Avatar asked Aug 21 '16 04:08

Zeke


1 Answers

Not sure if you ever figured it out.

When you create the new style and configure it, you have to change the name of the template to ".". This changes the root style for the treeview. You also need to specify a theme, even if it is default. So it would look something like:

s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('default')

Then when you create the treeview, you shouldn't have to specify a style at all.

Let me know if this works for you.

Edit: Since this is being downvoted for some reason, I'll clarify:

Code with the style part commented out:

    #s = ttk.Style()
    #s.configure(".", indicatorsize = '0')
    #s.theme_use('clam')

    j = ttk.Treeview(self.parent)
    j.place(relx = 0.5, rely = 0.5, anchor = "center")
    j.insert("",1,"jacob",text = "Jacob")
    j.insert("jacob",1,"marcus",text = "Marcus")
    j.insert("jacob",2,"tony",text = "Tony")
    j.insert("jacob",3,"ricardo",text = "Ricardo")

gives us

enter image description hereenter image description here

Code with the style part present

s = ttk.Style()
s.configure(".", indicatorsize = '0')
s.theme_use('clam')

j = ttk.Treeview(self.parent)
j.place(relx = 0.5, rely = 0.5, anchor = "center")
j.insert("",1,"jacob",text = "Jacob")
j.insert("jacob",1,"marcus",text = "Marcus")
j.insert("jacob",2,"tony",text = "Tony")
j.insert("jacob",3,"ricardo",text = "Ricardo")

enter image description hereenter image description here

Hope this helps.

EDIT 2: Added the s.theme_use('clam') line because you need to specify which theme you're using. It also works with classic and default, but for some reason doesn't work with the vista theme.

like image 126
RBuntu Avatar answered Nov 13 '22 18:11

RBuntu