Problem in tabels

Hello
i made a python tkinter project.
my project is a register panel and in this panel i have a tabel with three
columns called name familyname and age but in this position if i enter 1 name twice
(i mean for example if i entered martin name once and i again enter martin name) my
program adds the instructions again although i entered them once

from tkinter import ttk
from tkinter import *
from tkinter import messagebox

UserData = []

# Customize
Screen = Tk()
Screen.title("Panel")
Screen.iconbitmap("icon/icon.ico")
Screen.geometry("%dx%d+%d+%d" % (600, 300, 200, 200))
Screen.resizable(False, False)

# String Variable
name = StringVar()
family = StringVar()
age = StringVar()


# Function
def register(user):
    if int(user["age"]) < 18:
        messagebox.showerror("Error", "Your too Young")
    else:
        messagebox.showinfo("info", "Done!")
        UserData.append(user)
        return True


def Delete(e):
    for item in e:
        item.set("")


def RegisterClicked():
    NAME = name.get()
    FAMILY = family.get()
    AGE = age.get()

    if name.get() == "" or family.get() == "" or age.get() == "":
        messagebox.showwarning("Warning!", "please enter all of your instructions")
        return
    us = {"name": NAME, "family": FAMILY, "age": AGE}
    result = register(us)

    if result:
        list = [name, family, age]
        InsertData(list)
        Delete(list)
        Name.focus_set()


def InsertData(value):
    tbl.insert('', "end", value=[value[0].get(), value[1].get(), value[2].get()])


def Select(e):
    selection_row = tbl.selection()
    if selection_row != ():
        name.set(tbl.item(selection_row)["values"][0])
        family.set(tbl.item(selection_row)["values"][1])
        age.set(tbl.item(selection_row)["values"][2])


def Search(value):
    SecondList = []
    for item in UserData:
        if item["name"] == value or item["family"] == value or item["age"] == value:
            SecondList.append(item)
    return SecondList


def SearchClicked():
    query = search.get()
    result = Search(query)

    Clean()
    Load(result)


def Clean():
    for item in tbl.get_children():
        sel=(str(item),)
        tbl.delete(sel)

def Load(value):
    for item in value:
        tbl.insert('', "end", value=[item["name"], item["family"], item["age"]])



# Label
Label(Screen, text="Name:", font="normal 20 bold").place(x=0, y=0)
Label(Screen, text="FamilyName:", font="normal 20 bold").place(x=0, y=50)
Label(Screen, text="Age:", font="normal 20 bold").place(x=0, y=100)

# Entry
Name = Entry(Screen)
Name.configure(width=20, textvariable=name)
Name.place(x=90, y=10)

Family = Entry(Screen)
Family.configure(width=20, textvariable=family)
Family.place(x=180, y=60)

Age = Entry(Screen)
Age.configure(width=20, textvariable=age)
Age.place(x=90, y=110)

search = Entry(Screen)
search.configure(width=20)
search.place(x=160, y=205)
# Button
Register = Button(Screen, text="Register")
Register.configure(font="normal 10 bold", bg="gray", command=RegisterClicked)
Register.place(x=0, y=170)

SearchButton = Button(Screen, text="Search")
SearchButton.configure(font="normal 10 bold", bg="gray", command=SearchClicked)
SearchButton.place(x=100, y=200)
# Tabel
tbl = ttk.Treeview(Screen, columns="c1, c2, c3", show="headings")
tbl.bind("<Button-1>", Select)
tbl.column("# 1", width=50)
tbl.heading("# 1", text="Name")

tbl.column("# 2", width=90)
tbl.heading("# 2", text="FamilyName")

tbl.column("# 3", width=50)
tbl.heading("# 3", text="Age")
tbl.pack(side=RIGHT, fill=BOTH)
Screen.mainloop()

this is all of my code :point_up::point_up::point_up:

4

as you can see in the photo that i send you can see the john mactavish with 23 year
old is there again and i just want to make a script that it works like if all of instructionss were the same messagebox show something

i realy appreciate it if you help me

Hello,

for this, you will have to modify the list containing the UserData names - in your case, the UserData variable. One potential solution is to use a list, with dictionaries as elements, each having lists as values.

The idea is to have each list dictionary element in this format:

{f_name: [l_name, age]}

The dictionary key contains the first name, and the dictionary value is a two element list containing the family name (last name) and age. If entered in this format, ALL information may be referenced rather easily.

If you are not familiar with exception handling, please reference related material so that you become familiar with the associated concepts. A try / except exception pair is used in the iteration to escape when a match has been found between the latest name entered and the existing names already in the list. If a name match is found, an exception is raised manually to escape further iteration. The name_entry_flag is set to 1. This is used in the conditional statement to test if the latest name entered is already in the list.

Test and study the code carefully to see how can apply it to your particular use case.

# Create list containing names
UserData = []

# Populate list with hypothetical names for simulation purposes
f_name = 'John'; l_name = 'Knietel'; age = 34
temp = {f_name: [l_name, age]}
UserData.append(temp)

f_name = 'Sam'; l_name = 'Sutherland'; age = 37
temp = {f_name: [l_name, age]}
UserData.append(temp)

f_name = 'John'; l_name = 'Donaldson'; age = 25
temp = {f_name: [l_name, age]}
UserData.append(temp)

f_name = 'Rick'; l_name = 'Peters'; age = 29
temp = {f_name: [l_name, age]}
UserData.append(temp)

print('Starting list:')
print(UserData, sep='')

# Hypothetical new name being entered for testing purposes:
first_name = 'Ricky'
last_name = 'Donaldson'
age = 32
name_entry_flag = 0  # 0 = no match, 1 = there is a match

# Check if name already in list:
try:
    for entry in UserData:
        for key in entry.keys():
            if first_name == key and last_name == entry[first_name][0]:
                name_entry_flag = 1  # raise flag
                raise StopIteration
except StopIteration:
        print(f"\nName already entered: {first_name} {entry[first_name][0]}, Age: {entry[first_name][1]}")

# Enter new name to the list
if name_entry_flag == 0:
    temp = {first_name: [last_name, age]}
    UserData.append(temp)
    print('\nAdded name to the UserData list.')

print('\nEnding list:')
print(UserData, sep='')

Test the script by entering existing names in the list and by entering new names to get a sense of how it works.

Good luck!

Thanks for your help :slightly_smiling_face: