我试图查看在Entry小部件中输入的值是否存储在StringVar()对象中,但是当我打印字符串值对象的长度时,它会显示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()
答案 0 :(得分:0)
最小工作示例,在按StringVar
Entry
分配给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
会显示包含小部件的窗口,因此在启动len()
之前使用mainloop
毫无意义,因为var
仍为空。
编辑:,您的班级和职能在课堂外
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()
或者使用self.
并将函数放在类中作为方法
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()