My Program is not working in python Tkinter Search for table

Hi All i hope You had a fantastic day . i have code that in that code i made an Entry box
for Searching with Button it was made that for example if we click the search button while we wrote the correct name or family name we have to see the result of search but it doesnt work!
MyCode:

from tkinter import *
from tkinter import ttk

UserData = []

# Customize
Screen = Tk()
Screen.title("Test")
Screen.geometry("%dx%d+%d+%d" % (900, 450, 200, 200))
Screen.resizable(False, False)

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


# Functions


def BtnEnable(e):
    if Name.get() and Family.get() != "":
        Register.configure(state=NORMAL)
        UserData.append(e)
        return True
    else:
        Register.configure(state=DISABLED)
        return False


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


def Clear():
    NAME = Name.get()
    FAMILY = Family.get()

    us = {"name": NAME, "family": FAMILY}
    result = BtnEnable(us)
    if result:
        list = [name, family]
        insert(list)
        Delete(list)
        Name.focus_set()


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


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


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


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


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

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


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

# Entry
Name = Entry(Screen)
Name.bind("<KeyRelease>", BtnEnable)
Name.configure(width=20, justify="right", textvariable=name)
Name.place(x=390, y=110)

Family = Entry(Screen)
Family.bind("<KeyRelease>", BtnEnable)
Family.configure(width=20, justify="right", textvariable=family)
Family.place(x=480, y=160)

search = Entry(Screen)
search.configure(width=20)
search.place(x=160, y=50)
# Button
Register = Button(Screen, text="Register")
Register.configure(bg="aqua", font="normal 20 bold", state=DISABLED, command=Clear)
Register.place(x=300, y=300)

SearchButton = Button(Screen, text="Search")
SearchButton.configure(bg="gray", font="normal 20 bold", command=SearchClicked)
SearchButton.place(x=160, y=90)
# Tabel
tbl = ttk.Treeview(Screen, columns="c1, c2", 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.pack(side=LEFT, fill=BOTH)
Screen.mainloop()

this is all of my code and i have to say that i dont get any errors but my program doesnt work while i click on search button
if you help me i really Appreciate it
have an awsome day :grinning:

Hi,

I reviewed your script and found that for some oddball reason, the following line is corrupting the variable e in the BtnEnable function. It then corrupts the UserData variable because you’re appending the value of e to it. You can print these variables in their respective locations by adding print statements and observing their values for confirmation.

Family.bind("<KeyRelease>", BtnEnable) # comment out this line

Why are you using a list for this anyway? Why not use a dictionary instead. This way you pair the name and family name into a key / value pair which is just as easy to use. For example, let’s create a blank dictionary to start.

names = {}

Then, every time that you would like to add a name (i.e., when you press the register button), you do so as:

# generically using variable as: names[name] = family
names['James'] = 'Donaldson'
names['Saray'] = 'Scott'
names['David'] = 'Johnson'

if names['James'] == 'Donaldson':
    print('There is a first name and family name match.')

Note that you can also adjust the window size:
adjust_window_size

The way that you’re currently entering the values into the list Userdata, the result is (after commenting out the line highlighted above of course):

Userdata =  [{'name': 'James', 'family': 'Donaldson'}, {'name': 'Larry', 'family': 'David'}, {'name': 'Rich', 'family': 'Samuelson'}, {'name': 'Sarah', 'family': 'Schultz'}, {'name': 'Sam', 'family': 'Waltz'}]

You basically have a list of dictionary entries all having the same key and family string variable key names. This is a bug in your script and why I propose using only a dictionary since then the names would have a unique name / family pair per the example above.

FYI, while this may work properly as written, comparisons aren’t grouped. This isn’t comparing both Name.get() and Family.get() against "", it’s checking the first for “truthiness” and then comparing the second to "". Effectively:

if Name.get():
    if Family.get() != "":

If you want two comparisons, you have to state them explicitly, or use a tool to repeat it.

if Name.get() != "" and Family.get() != "":

thanks for your assist i really appreciate it :blush:

after that i did not give it the e i mean i did not give the User Data e the table erases but it doesnt load my Search information

Hello,

what type is your UserData variable? Did you change it to a dictionary type or is it still of type list?

its still list i found an issue in Preformatted textmy code and that was that i needed to just make diffrent functons from Erase And Add i needed to put a funtion for erase and a function for Add after that i did that i needed to use it in Search Clicked as

Erase()
Add(result)

but it still didnt work

Using a list is the problem as I stated in the previous post. Using a dictionary fixes this bug - add each name / family name as I described above. As per the screenshot, I was able to easily add the names using this method.

As a quick test. Run this line:

print(UserData)

Can you please provide what is printed … after adding at least three different names for testing purposes - even if they are not shown in the widget.