且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

插入带有标签的行时,Tkinter Treeview问题

更新时间:2022-06-19 08:28:01

该问题似乎是由更新版本的tkinter引起的,而不是更新版本的Python引起的.在 https://bugs.python.org/issue36468

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