Tkinter Treeview issue when inserting rows with tags

前端 未结 4 1183
臣服心动
臣服心动 2021-01-20 08:10

I have just switched from python 3.6 to python 3.7. I have a function which inserts rows in a Treeview tree with tags. The tags are used for giving a foreground color and a

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 08:49

    Looks like the issue was caused by a newer version of tkinter, not a newer version of Python. This was reported in https://bugs.python.org/issue36468 and https://core.tcl-lang.org/tk/info/509cafafae

    Here is a proposed solution. It should be both backward and forward compatible:

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

提交回复
热议问题