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