0

I created a window in Tkinter. With a combobox you select a name (from a SQLite3 database). It shows other data from this row in the database (email etc). However: when I choose another name in the combobox, earlier data is still visible (see image).

earlier data is still visible

1

I tried to fix this to set the variable with text="" (nothing) (see first rows in the def ZoekNAW():, but it doesn't work.

I hope someone can help me :)

#setting data for combobox: retrieve names from database
def windowNAW():
    gebruikers = []
    sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
    cursor = sqliteConnection.cursor()
    cursor.execute("SELECT achternaam FROM gebruikers")
    for row in cursor.fetchall():
        gebruikers.append(row[0])
    cursor.close()
    sqliteConnection.close()

#show other data from selected user (get email etc from database)
    def ZoekNAW():
        emailZk = Label(windowNAW, text="", font="Verdana 10 bold").place(x=200, y=233)
        adresZk = Label(windowNAW, text="", font="Verdana 10 bold").place(x=200, y=263)
        woonplaatsZk = Label(windowNAW, text="", font="Verdana 10 bold").place(x=200, y=293)
        ZoekGebruiker = StringVar()
        ZoekGebruiker = CBZoekGebruiker.get()
        sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
        cursor = sqliteConnection.cursor()
        cursor.execute(f"SELECT * FROM gebruikers WHERE Achternaam='{ZoekGebruiker}'")
        rows = cursor.fetchall()
        for data in rows:
            emailZk = Label(windowNAW, text=f"{data[1]}", font="Verdana 10 bold")
            emailZk.place(x=200, y=233)

            adresZk = Label(windowNAW, text=f"{data[4]}", font="Verdana 10 bold")
            adresZk.place(x=200, y=263)

            woonplaatsZk = Label(windowNAW, text=f"{data[5]}", font="Verdana 10 bold")
            woonplaatsZk.place(x=200, y=293)
        cursor.close()
        sqliteConnection.close()

    windowNAW = Tk()
    windowNAW.title("Gegevens bij gebruikers zoeken")   
    windowNAW.geometry("500x500")
    
    IntroNAW = Label(windowNAW, text="Zoek gegevens bij bestaande gebruikers. Selecteer een gebruiker.", font='Verdana 10')
    IntroNAW.place(x=10, y=60)

    CBZoekGebruiker = ttk.Combobox(windowNAW, values=gebruikers, state="readonly")
    CBZoekGebruiker.place(x=10, y=100)

    Button(windowNAW, text="Zoek", command=ZoekNAW).place(x=10, y=150)

    email = Label(windowNAW, text="Email: ", font="Verdana 10 bold")
    email.place(x=10, y=230)

    adres = Label(windowNAW, text="Adres: ", font="Verdana 10 bold")
    adres.place(x=10, y=260)

    woonplaats = Label(windowNAW, text="Woonplaats: ", font="Verdana 10 bold")
    woonplaats.place(x=10, y=290)

    windowNAW.mainloop()
vimuth
  • 5,064
  • 33
  • 79
  • 116
Djonie
  • 3
  • 3

1 Answers1

0

Thank you, toyota Supra.

I understand now that I shouldn't have set the Labels in the function. It was necessary to create variables (StringVar) for the labels and in the function change the values of these StringVar-variables. With the change in values, the Labels were updated. I place the new code underneed, should anyone be interested.

def windowNAW():
gebruikers = []
sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
cursor = sqliteConnection.cursor()
cursor.execute("SELECT achternaam FROM gebruikers")
for row in cursor.fetchall():
    gebruikers.append(row[0])
cursor.close()
sqliteConnection.close()

def ZoekNAW():
    ZoekGebruiker = StringVar()
    ZoekGebruiker = CBZoekGebruiker.get()
    sqliteConnection = sqlite3.connect('ZHSys-NAW.db')
    cursor = sqliteConnection.cursor()
    cursor.execute(f"SELECT * FROM gebruikers WHERE Achternaam='{ZoekGebruiker}'")
    rows = cursor.fetchall()
    for data in rows:
        emailDB = f"{data[1]}"
        adresDB = f"{data[4]}"
        woonplaatsDB = f"{data[5]}"
    emailGet.set(emailDB)
    adresGet.set(adresDB)
    woonplaatsGet.set(woonplaatsDB)
    cursor.close()
    sqliteConnection.close()

windowNAW = Tk()
windowNAW.title("Gegevens bij gebruikers zoeken")   
windowNAW.geometry("500x500")

emailGet = StringVar()
emailGet.set("")
adresGet = StringVar()
adresGet.set("")
woonplaatsGet = StringVar()
woonplaatsGet.set("")

IntroNAW = Label(windowNAW, text="Zoek gegevens bij bestaande gebruikers. Selecteer een gebruiker.", font='Verdana 10')
IntroNAW.place(x=10, y=60)

CBZoekGebruiker = ttk.Combobox(windowNAW, values=gebruikers, state="readonly")
CBZoekGebruiker.place(x=10, y=100)

Button(windowNAW, text="Zoek", command=ZoekNAW).place(x=10, y=150)

email = Label(windowNAW, text="Email: ", font="Verdana 10 bold")
email.place(x=10, y=230)

emailZk = Label(windowNAW, textvariable=emailGet, font="Verdana 10 bold")
emailZk.place(x=200, y=230)

adres = Label(windowNAW, text="Adres: ", font="Verdana 10 bold")
adres.place(x=10, y=260)

adresZk = Label(windowNAW, textvariable=adresGet, font="Verdana 10 bold")
adresZk.place(x=200, y=260)

woonplaats = Label(windowNAW, text="Woonplaats: ", font="Verdana 10 bold")
woonplaats.place(x=10, y=290)

woonplaatsZk = Label(windowNAW, textvariable=woonplaatsGet, font="Verdana 10 bold")
woonplaatsZk.place(x=200, y=290)

windowNAW.mainloop()
Djonie
  • 3
  • 3