Skip to content
Advertisement

_tkinter.TclError: Item 1 already exists with tkinter treeview

I creating GUI interacting with treeview and I want to get all the value inside the list and show it in treeview window. I have a add button and once I click it, it will work fine. But on the second time, it shows an error Item 1 already exists. It also does not added to the list.

This is my code:

from tkinter import *
from tkinter import ttk

root = Tk()

def add1():
    global count
    for i in tree_table.get_children():
        tree_table.delete(i)

    get_name = txt_name.get()
    get_ref = txt_ref.get()
    get_age = txt_age.get()
    get_email = txt_email.get()
    data_list.append((get_name, get_ref, get_age, get_email))

    for item in data_list:
        tree_table.insert(parent='', index='end', iid=count, text=f'{count + 1}', values=(item))
    txt_name.delete(0, END)
    txt_ref.delete(0, END)
    txt_age.delete(0, END)
    txt_email.delete(0, END)
    count += 1
    print(data_list)


tree_table = ttk.Treeview(root)
global count
count = 0

data_list = []
tree_table['columns'] = ("Name", "Reference No.", "Age", "Email Address")

tree_table.column("#0", width=30)
tree_table.column("Name", width=120, anchor=W)
tree_table.column("Reference No.", width=120, anchor=W)
tree_table.column("Age", width=120, anchor=W)
tree_table.column("Email Address", width=120, anchor=W)

headings = ["#0", "Name", "Reference No.", "Age", "Email Address"]
txt_headings = ["No.", "Name", "Reference No.", "Age", "Email Address"]
for i in range(len(headings)):
    tree_table.heading(headings[i], text=txt_headings[i], anchor=W)


txt_name = Entry(root, width=20)
txt_name.pack()
txt_ref = Entry(root, width=20)
txt_ref.pack()
txt_age = Entry(root, width=20)
txt_age.pack()
txt_email = Entry(root, width=20)
txt_email.pack()
btn_enter = Button(root, text="Add", width=20, command=add1)
btn_enter.pack()

tree_table.pack()
root.mainloop()

Traceback:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:PythonPython385libtkinter__init__.py", line 1883, in __call__
    return self.func(*args)
  File "d:/Code/Cpet5/new.py", line 32, in add1
    tree_table.insert(parent='', index='end', iid=count, text=f'{count + 1}', values=(item))
  File "C:PythonPython385libtkinterttk.py", line 1366, in insert
    res = self.tk.call(self._w, "insert", parent, index,
_tkinter.TclError: Item 1 already exists

How do I refresh the treeview to reflect the changes made in the list?

Advertisement

Answer

Quick fix:

Get rid of the iid argument:

tree_table.insert(parent='', index='end', text=f'{count + 1}', values=(item))

But that will create an issue of the No. column getting same value always.

Actual fix:

So the actual problem is your list contains alot of values, you should get rid of those values once you insert it, so the code should be something like:

def add1():
    global count

    get_name = txt_name.get()
    get_ref = txt_ref.get()
    get_age = txt_age.get()
    get_email = txt_email.get()

    data_list.clear() #clear the list 
    data_list.append((get_name, get_ref, get_age, get_email)) #append to the empty list

    for item in data_list:
        tree_table.insert(parent='', index='end',iid=count, text=f'{count + 1}', values=(item))

    txt_name.delete(0, END)
    txt_ref.delete(0, END)
    txt_age.delete(0, END)
    txt_email.delete(0, END)
    count += 1

    print(data_list)

This way your clearing whats inside of the list each time and appending new values to list. With your older code you can see that first time the the list is [('1', '1', '1', '1')] and when the next set of values is appended it becomes [('1', '1', '1', '1'), ('2', '2', '2', '2')] so there is not enough space accommodate all this value in? Anyway this fixes it. Or you could also make a tuple of those inputs and then pass that tuple as the values for treeview without looping, like acw1668 did.

Advertisement