[英]tkinter TreeView: disable selection highlight
我有一個 tkinter TreeView 並使用標簽來標記多個選定的項目,如下圖黃色所示。 不幸的是,無論標簽如何,最后點擊的項目將始終顯示為藍色。 如何防止 TreeView 為上次單擊的項目着色?
這是我用來選擇項目的代碼片段:
def on_click(self, event):
"""Set tag for selected datasets."""
# Remove 'plotted' tag if existent
if 'plotted' in self.tree.item(self.tree.selection())['tags']:
# FIXME: Only remove tag 'plotted'
self.tree.item(self.tree.selection(), tags=())
# Select only items that have no children
elif not self.tree.get_children(self.tree.selection()):
self.tree.item(self.tree.selection(), tags='plotted')
self.tree.column('#0', anchor=tk.E)
這是它的外觀示例。 請注意,您無法判斷 DNTP_iDL_... 是否應該是白色的(b/c 它是黃色的,我單擊它以停用它):
我知道這是舊的,但我沒有找到真正的答案。
經過一番挖掘 -這是可能的(不明顯但可能):
顯然,treeview 有單獨的系統來着色 - 通過樣式和通過標簽(使用 tree.tag_bind 更動態地添加基本時尚的東西)。 但是,我的測試表明,如果您單擊該項目,則標簽在框中不起作用(此問題的主要問題)。
這是我想出的解決方案:
root = tk.Tk()
style = ttk.Style(root)
style.theme_use("clam")
style.map("Treeview",
background=[
('selected', '!focus', 'yellow'),
])
# here is your treeview widgets and stuff
root.mainloop()
可能有更優雅的解決方案......但我沒有找到。
希望它可以幫助某人!
更新
我發現當您單擊該項目然后轉到另一個應用程序(alt+Tab)時,您將看到黃色的選擇。
我通過將背景參數更改為:
# ('selected', '!focus', 'yellow'),
('selected', 'invalid', 'yellow'),
事實證明,我在使用標簽選擇多個項目時走錯了路。
只需使用內置選擇(使用 shift- 或 cmd-select),然后使用mytree.selection()
來獲取所有選定項目的列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.