Reputation: 63
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()
Upvotes: 1
Views: 6740
Reputation: 142641
Minimal working example which displays length of StringVar
assigned to Entry
after pressing Button
import tkinter as tk
# --- functions ---
def check():
print('len:', len(var.get()))
# --- main ---
root = tk.Tk()
var = tk.StringVar()
ent = tk.Entry(root, textvariable=var)
ent.pack()
but = tk.Button(root, text="Check", command=check)
but.pack()
root.mainloop()
mainloop
displays window with widgets so using len()
before starting mainloop
makes no sense because var
is still empty.
EDIT: with your class and function outside class
import tkinter as tk
from tkinter import ttk
# --- classes ---
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
frame5 = tk.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 = tk.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)
# --- functions ---
def updateCustomerList(barCode):
print(len(barCode.get()))
# --- main ---
root = tk.Tk()
main = MainPage(root, root)
main.pack()
root.mainloop()
Or using self.
and putting function inside class as method
import tkinter as tk
from tkinter import ttk
# --- classes ---
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
frame5 = tk.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")
self.barCode = tk.StringVar()
frame5EntryBox = ttk.Entry(frame5, textvariable=self.barCode, width=40)
frame5EntryBox.grid(row=0, column=1, padx=5, pady=5)
frame5Button = ttk.Button(frame5, text="Add Item", command=self.updateCustomerList)
frame5Button.grid(row=0, column=2, padx=130, pady=10)
def updateCustomerList(self):
print(len(self.barCode.get()))
# --- functions ---
# --- main ---
root = tk.Tk()
main = MainPage(root, root)
main.pack()
root.mainloop()
Upvotes: 1