How to change the focus styling of a tkinter ttk.T

2019-02-28 14:13发布

Even though my Treeview has the option takefocue=False, the text in the cells is still taking focus somehow. Specifically the text column when I do tree.insert('', tk.END, text='Some Text', values=5) is taking focus, meaning there is a dashed line around Some Text. I was able to find this resource, but I am not sure of the layout string I need to change.

1条回答
We Are One
2楼-- · 2019-02-28 15:01

According to the Tcl/Tk wiki, the following 5 styles can be used to customize a ttk.Treeview() widget:

"Treeview"
"Treeview.Heading"
"Treeview.Row"
"Treeview.Cell"
"Treeview.Item"

Using .layout(), you can retrieve the layout specifications of each style:

style = ttk.Style()
style.layout("Treeview.Item") 

It turns out that the "Treeview.Item" style has a "Treeitem.focus" layout mark. If you comment it out when overwriting the layout, the focus drawing behavior (and the dashed line) will disappear:

style = ttk.Style()

style.layout("Treeview.Item",
[('Treeitem.padding', {'sticky': 'nswe', 'children': 
    [('Treeitem.indicator', {'side': 'left', 'sticky': ''}),
    ('Treeitem.image', {'side': 'left', 'sticky': ''}),
    #('Treeitem.focus', {'side': 'left', 'sticky': '', 'children': [
         ('Treeitem.text', {'side': 'left', 'sticky': ''}),
    #]})
    ],
})]
)

enter image description here

查看更多
登录 后发表回答