我认为您需要调用tkinter 不提供的一些互联网 tcl 小部件命令(如 cmets 中所述)。为此,我们使用tk.call 方法。我创建了两个函数并手动将这些函数分配给Treeview 对象:
def add_tag(new_tag,iid):
root.tk.call(tree,'tag','add',new_tag,iid)
def replace_tag(new_tag,to_be_replaced,iid=''):
"""If iid is ommited, replaces the old_tag from all the items if it exists"""
if iid:
root.tk.call(tree,'tag','add',new_tag,iid)
root.tk.call(tree,'tag','remove',to_be_replaced,iid)
else:
iids = tree.tag_has(to_be_replaced)
root.tk.call(tree,'tag','remove',to_be_replaced)
for i in iids:
root.tk.call(tree,'tag','add',new_tag,i)
def delete_tag(tag_to_delete,iid=''):
"""If iid is ommited, deletes all the tag from all items"""
if iid:
root.tk.call(tree,'tag','remove',tag_to_delete,iid)
else:
root.tk.call(tree,'tag','remove',tag_to_delete)
tree.add_tag = add_tag
tree.replace_tag = replace_tag
tree.delete_tag = delete_tag
iids = tree.tag_has('OLDTAG')
# Adding a tag to the first item in the treeview
tree.add_tag('NEWTAG',iids[0]) # Choosing the first item
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('OLDTAG', 'NEWTAG')
# Replacing the OLDTAG for all items
tree.replace_tag('OLDEST','OLDTAG')
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('NEWTAG', 'OLDEST')
# Delete NEWTAG for the first item
tree.delete_tag('NEWTAG',iids[0])
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('OLDEST',)
虽然创建自己的类会更好。请注意,“OLDTAG”是您预先为物品提供的任何标签。它并非完全万无一失,因此请随意玩耍。
没有任何事件驱动程序的整个代码:
from tkinter import *
from tkinter import ttk
root = Tk()
tree = ttk.Treeview(root,columns=('No.','Name'),show='headings')
tree.pack()
def add_tag(new_tag,iid):
root.tk.call(tree,'tag','add',new_tag,iid)
def replace_tag(new_tag,to_be_replaced,iid=''):
"""If iid is not specified replaces the old_tag from all the items if it exists"""
if iid:
root.tk.call(tree,'tag','add',new_tag,iid)
root.tk.call(tree,'tag','remove',to_be_replaced,iid)
else:
iids = tree.tag_has(to_be_replaced)
root.tk.call(tree,'tag','remove',to_be_replaced)
for i in iids:
root.tk.call(tree,'tag','add',new_tag,i)
def delete_tag(tag_to_delete,iid=''):
"""If iid is ommited, deletes all the tag from all items"""
if iid:
root.tk.call(tree,'tag','remove',tag_to_delete,iid)
else:
root.tk.call(tree,'tag','remove',tag_to_delete)
lst = [[1,'Me'],[2,'Myself'],[3,'I']]
for i in ('No.','Name'):
tree.heading(i,text=i)
tree.column(i,width=100)
for i in lst:
tree.insert('','end',values=i,tags='OLDTAG')
iids = tree.tag_has('OLDTAG')
tree.add_tag = add_tag
tree.replace_tag = replace_tag
tree.delete_tag = delete_tag
# Adding a tag to the first item in the treeview
tree.add_tag('NEWTAG',iids[0]) # Choosing the first item
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('OLDTAG', 'NEWTAG')
# Replacing the OLDTAG for all items
tree.replace_tag('OLDEST','OLDTAG')
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('NEWTAG', 'OLDEST')
# Delete NEWTAG for the first item
tree.delete_tag('NEWTAG',iids[0])
all_tags = tree.item(iids[0],'tags')
print(f'All tag for the given iid: {all_tags}') # ('OLDEST',)
root.mainloop()