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

后端 未结 2 457
鱼传尺愫
鱼传尺愫 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:10

    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.

    0 讨论(0)
  • 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'))
    
    0 讨论(0)
提交回复
热议问题