首页 > 解决方案 > 使用 tkinter 树视图小部件和页面 gui 构建器的问题

问题描述

我是 python 新手,正在尝试使用 PAGE GUI 构建器构建 GUI 应用程序我的页面上有一个 treeView 小部件,这是我在支持模块中的函数:

def initTree():
    for i in w.prefTreeviewScTrv.get_children():
        w.prefTreeviewScTrv.delete(i)
    w.prefTreeviewScTrv["columns"]=("one","two","three")
    w.prefTreeviewScTrv.column("#0", width=270, minwidth=270, stretch=tk.NO)
    w.prefTreeviewScTrv.column("one", width=150, minwidth=150, stretch=tk.NO)
    w.prefTreeviewScTrv.column("two", width=400, minwidth=200)
    w.prefTreeviewScTrv.column("three", width=80, minwidth=50, stretch=tk.NO)
    w.prefTreeviewScTrv.heading("#0",text="Name",anchor=tk.W)
    w.prefTreeviewScTrv.heading("one", text="Date modified",anchor=tk.W)
    w.prefTreeviewScTrv.heading("two", text="Type",anchor=tk.W)
    w.prefTreeviewScTrv.heading("three", text="Size",anchor=tk.W)
    # Level 1
    w.prefTreeviewScTrv.insert("", 1, "", text="first_file.txt", values=("22-Jun-16 11:25","first file","0.5 KB"))
    folder1=w.prefTreeviewScTrv.insert("", 2, "", text="Folder 1", values=("23-Jun-17 11:05","File folder",""))
    w.prefTreeviewScTrv.insert("", 3, "", text="text_file.txt", values=("23-Jun-17 11:25","TXT file","1 KB"))
    # Level 2
    w.prefTreeviewScTrv.insert(folder1, "end", "", text="photo1.png", values=("23-Jun-17 11:28","PNG file","2.6 KB"))
    w.prefTreeviewScTrv.insert(folder1, "end", "", text="photo2.png", values=("23-Jun-17 11:29","PNG file","3.2 KB"))
    w.prefTreeviewScTrv.insert(folder1, "end", "", text="photo3.png", values=("23-Jun-17 11:30","PNG file","3.1 KB"))

每当我运行程序时,它都会运行并正确创建树列和标题,但是在使用 insert 方法时,无论我给项目 0 或 1 提供什么索引,都会出现异常:

  Traceback (most recent call last):
    File "C:\page\tenders\notebook_support.py", line 127, in <module>
      notebook.vp_start_gui()
    File "C:\page\tenders\notebook.py", line 30, in vp_start_gui
      notebook_support.init(root, top)
    File "C:\page\tenders\notebook_support.py", line 115, in init
      initTree()
    File "C:\page\tenders\notebook_support.py", line 102, in initTree
      w.prefTreeviewScTrv.insert("", 1, "", text="first_file.txt", values=("22-Jun-16 11:25","first file","0.5 KB"))
    File "C:\Users\ori\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 1365, in insert
      res = self.tk.call(self._w, "insert", parent, index, _tkinter.TclError: Item  already exists

我究竟做错了什么?

标签: pythonpython-3.xtkinter

解决方案


问题是每次插入项目时,都将其 ID 设置为"". 这是根项目的 ID。因为不同的项目必须有不同的 ID,所以这是行不通的。您需要删除所有调用中的第三个参数insert()

# Level 1
w.prefTreeviewScTrv.insert("", 1, text="first_file.txt", values=("22-Jun-16 11:25","first file","0.5 KB"))
folder1=w.prefTreeviewScTrv.insert("", 2, text="Folder 1", values=("23-Jun-17 11:05","File folder",""))
w.prefTreeviewScTrv.insert("", 3, text="text_file.txt", values=("23-Jun-17 11:25","TXT file","1 KB"))
# Level 2
w.prefTreeviewScTrv.insert(folder1, "end", text="photo1.png", values=("23-Jun-17 11:28","PNG file","2.6 KB"))
w.prefTreeviewScTrv.insert(folder1, "end", text="photo2.png", values=("23-Jun-17 11:29","PNG file","3.2 KB"))
w.prefTreeviewScTrv.insert(folder1, "end", text="photo3.png", values=("23-Jun-17 11:30","PNG file","3.1 KB"))

推荐阅读