如何覆盖小部件树视图中的子行?

2024-10-03 23:29:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试覆盖treeview表中父级中的子级。 我尝试过的一种方法是删除并插入新的子行,但在运行代码后,我发现错误:

_tkinter.TclError: Item I003 I004 I005 I006 not found

在我出现这个错误之前,删除和插入新的子项大约工作了5次。从我可以看出,一旦所有的孩子都被添加和删除,一旦我得到上面的错误。就像iid一旦被删除就不会被重置一样

我需要选择一个特定的子项并覆盖它,或者删除一个子项并在其位置插入一个新的子项

这是我的一些代码,我在其中获取所选选项卡中的子项,删除它们并插入新的子项

tree_select = tree.focus()
clear_tree_children = tree.get_children(tree_select)

clear_tree_children = tree.get_children(tree_select)
        if len(clear_tree_children) > 0:
            for child in clear_tree_children:
                tree.delete(clear_tree_children)
                print(child)

if tree_phys.get() == 1:
            disPhys = ("", "", "", "", "", "Physical Disability")
            tree.insert(parent=tree_select, index=END, values=disPhys)

有人对我如何解决这个问题有什么想法或其他方法吗

谢谢,雅各布


Tags: 方法代码childtreegetif错误select
2条回答

It is like the iid isn't reset once they are deleted.

是的,确切地说,tkinter以增量方式生成默认iid,而不关心之前的某些项目是否已被删除。如果您需要删除/插入项目,并且仍然能够使用相同的iid访问它们,则在创建项目时应为自己分配iid:

tree.insert(parent, index, my_iid, values=...)

如果您只想更改给定单元格中的值,JacksonPro的答案非常有效。但是,如果要同时更改整行,可以使用

tree.item(iid, values=new_values, text=new_text)

下面是一个示例,当双击整行时,整行都会发生更改:

import tkinter as tk
from tkinter import ttk
from random import randrange

def click(event):
    iid = tree.focus()  # get selected item
    tree.item(iid, text='%s - reset' % iid, values=[0, 0])

columns = ['col1', 'col2']
root = tk.Tk()
tree = ttk.Treeview(root, columns=columns)
for col in columns:
    tree.heading(col, text=col)
tree.pack()

for i in range(10):
    tree.insert('', 'end', 'item%i' % i, text='item%i' % i, values=[randrange(0, 10) for c in columns])

tree.bind('<Double-1>', click)
root.mainloop()

如果要覆盖选定行,可以使用.set(iid, column, value)

下面是我不久前编写的示例代码(双击任意一行):

from tkinter import ttk 
import tkinter as tk 

titles={'Id': [1,2,3,4,5, 6, 7, 8, 9], 'Names':['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Kim', 'Kim', 'Kim', 'Kim']}

def update(selected_index_iid, changed):
    index = treev.index(selected_index_iid)# or just index = treev.index(treev.selection())

    treev.set(selected_index_iid, 1, changed) # updating the tree
    titles['Names'][index] = changed  #updating the dictionary
    print(titles)

def clicked(event):
    global titles
    top = tk.Toplevel(window)

    label = tk.Label(top, text='Update: ')
    label.pack()

    entry = tk.Entry(top)
    entry.insert(0, treev.set(treev.selection())['1']) #if you only specify the iid 'set' will return dict of items, ['1'] is to select 1st column
    entry.pack()

    button= tk.Button(top, text='Update', command=lambda :update(treev.selection(), entry.get()))
    button.pack()
    
    
  
window = tk.Tk() 

treev = ttk.Treeview(window, selectmode ='browse')
treev.bind('<Double-Button-1>', clicked)

treev.pack(side='left',expand=True, fill='both') 
  

verscrlbar = ttk.Scrollbar(window,  
                           orient ="vertical",  
                           command = treev.yview) 
  
verscrlbar.pack(side ='right', fill ='y')   
treev.configure(yscrollcommand = verscrlbar.set) 

treev["columns"] = list(x for x in range(len(list(titles.keys()))))
treev['show'] = 'headings'

  
for x, y in enumerate(titles.keys()):
    treev.column(x, minwidth=20, stretch=True,  anchor='c')
    treev.heading(x, text=y)

for args in zip(*list(titles.values())):
    treev.insert("", 'end', values =args) 

window.mainloop() 

相关问题 更多 >