0

I am new to python and trying to build a GUI app using PAGE GUI builder I have a treeView widget on my page and here is my function in the support module:

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"))

Whenever I run the program, it runs and create the tree columns and heading properly but I get an exception when using the insert method no matter what index I give the item 0 or 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

what am I doing wrong?

2
  • What is the code you're issuing to insert an item? Commented Feb 8, 2020 at 18:27
  • You are defining: w.prefTreeviewScTrv["columns"]=("one","two","three"): but it seems that there are 4 columns. Have you forgotten the first column? Commented Feb 8, 2020 at 18:29

1 Answer 1

3

The problem is that every time you insert an item, you set its ID to "". This is the ID of the root item. Because different items must have different IDs, this doesn't work. You need to remove the third argument in all calls to 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"))
Sign up to request clarification or add additional context in comments.

1 Comment

Or, the OP can generate valid IDs…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.