I'm trying to see whether or not a value entered in the Entry widget is stored inside the StringVar() object but when I print the length of the string value object, it says 0.
class POS(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self, *args, **kwargs)
"""
some code to build multiple frames here
"""
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
frame5 = Frame(self, bg = "pink")
frame5.pack(fill = BOTH)
frame5Label1 = tk.Label(frame5, text = "Product Bar Code", font = NORMAL_FONT)
frame5Label1.grid(row = 0, column = 0, padx=5, pady=5, sticky = W)
barCode = StringVar()
frame5EntryBox = ttk.Entry(frame5, textvariable = barCode, width = 40)
frame5EntryBox.grid(row = 0, column = 1, padx = 5, pady = 5)
frame5Button = ttk.Button(frame5, text = "Add Item", command = lambda: updateCustomerList(barCode))
frame5Button.grid(row = 0, column = 2, padx = 130, pady = 10)
def updateCustomerList(barCode):
print(len(barCode.get()))
app = POS()
app.geometry("700x700")
app.resizable(False,False)
app.mainloop()
printinside that function.mainloop()starts program so yourlen()is executed even before tkinter displays window and whenframe5EntryBoxandbarCodeare still empty. Assign to button normal function withprint(len(barCode.get())). And don't forget to put some text inframe5EntryBoxbefore you press button.