使用tkinter treeview小部件和页面gui生成器时出现问题

2024-09-30 04:40:09 发布

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

我是python新手,正在尝试使用pageguibuilder构建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

我做错了什么


Tags: textinpykblinecolumnwidthjun
1条回答
网友
1楼 · 发布于 2024-09-30 04:40:09

问题是每次插入项目时,都将其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"))

相关问题 更多 >

    热门问题