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

后端 未结 2 542
鱼传尺愫
鱼传尺愫 2021-01-23 06:37

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 b

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 07:31

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

提交回复
热议问题