Been working on a pesronal app and one of my TopLevel windows won´t work correctly. I need for the variable idd to be whatever the user inputs in the Entry widget, but something is wrong and I've tried everything I know of tkinter without results.
The widget I generate is the following:
def win_erase_id(parent):
idd=-1
wind=Toplevel(parent)
wind.title("Enter id")
lbl=Label(win,text="Enter player ID you want to erase:")
svar=StringVar()
ent=Entry(win,textvariable=svar)
fr=Frame(win)
def butA_m():
idd=int(svar.get())
wind.destroy()
def butC_m():
idd=-1
wind.destroy()
butA=Button(fr,text="Accept",command=butA_m,relief=RAISED)
butC=Button(fr,text="Cancel",command=butC_m,relief=RAISED)
lbl.grid(row=0,sticky="nswe")
ent.grid(row=1,sticky="nswe")
fr.grid(row=2,sticky="nswe")
butA.pack(side=LEFT,fill=BOTH)
butC.pack(side=RIGHT,fill=BOTH)
wind.wait_window()
return idd
I thought that by setting a textvariable and then in the method butA_m asking that variable for its value the end variable would be the integer I needed, but the value I get is always -1. Tried using .delete() and .insert() on the Entry widget but wouldn't work either.
Edit: I accidentally erased some "d"s from the wind
instances but already corrected it.
nonlocal, as stated by somebody before me actually solves it, I just did not understand how to use it, but after checking this site I ended up correcting my code and it worked.
def butA_m(): nonlocal idd idd=int(svar.get()) wind.destroy() def butC_m(): nonlocal idd idd=-1 wind.destroy()