如何修改(例如添加/洗掉)tagsa 中選項的值ttk.Treeview?我可以使用任何內置的 tkinter/widget 方法嗎?
...
columns = list(range(1,3))
tree = ttk.Treeview(root, columns=columns)
tree.insert('', 'end', iid=1, values=(1,1), tags='1')
...
- 如何將值“NEWTAG”添加到
tags='1'?tags應該具有值“1”和“NEWTAG”。 - 如何用“NEWTAG”添加替換“1”?
tags值最終應該是“NEWTAG”。 - 如果 的值
tags已經是“1”和“NEWTAG”,我該如何洗掉“NEWTAG”以便tags="1"?
uj5u.com熱心網友回復:
Treeview 為標簽獲取 tupels 并且可以添加或反之亦然,如下所示:
import tkinter as tk
from tkinter import ttk
def add_tag_to_tag(old,new):
lst = tree.tag_has(old) #returns list of iid's
for iid in lst:
old_tags = tree.item(iid)['tags']
tree.item(iid,tags=(old_tags,new))
root = tk.Tk()
tree = ttk.Treeview(root,columns=())
tree.pack(fill='x')
for i in range(5):
param = tree['columns'] (f'#{i}',)
tree.configure(columns=param)
for i in range(5):
tree.heading(column=f'#{i}',text=f'text{i}',anchor='w')
tree.column(column=f'#{i}', width=150,minwidth=50,stretch=False)
for i in range(5):
tree.insert('','end',iid=i,values=(i,),tags=(i,),text=str(i))
add_tag_to_tag('1','NEW')
tree.bind('<Button-1>', lambda e:print(tree.item(tree.focus())))
uj5u.com熱心網友回復:
我認為您需要呼叫一些tkinter未提供的Internet tcl 小部件命令(如評論中所述)。為此,我們使用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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358521.html
