如何让 Entry Widget 传递给函数?
How do I get the Entry Widget to pass into a function?
我确定这是一个简单的错误,我已将其定位到代码中的特定位置:
class NewProduct(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tLabel = ttk.Label(self, text="Title: ", font=NORM_FONT).grid(row=0, padx=5, pady=5)
qLabel = ttk.Label(self, text="Quantity: ", font=NORM_FONT).grid(row=1, padx=5, pady=5)
pLabel = ttk.Label(self, text="Price: $", font=NORM_FONT).grid(row=2, padx=5, pady=5)
te = ttk.Entry(self).grid(row=0, column=1, padx=5, pady=5) # Add validation in the future
qe = ttk.Entry(self).grid(row=1, column=1, padx=5, pady=5)
pe = ttk.Entry(self).grid(row=2, column=1, padx=5, pady=5)
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(self.te.get(), self.qe.get(), self.pe.get()))
#WHY IS THIS WRONG!!!!!???!?!?!?!?!?
saveButton.grid(row=4, padx=5)
cancelButton = ttk.Button(self, text="Cancel", command=lambda: popupmsg("Not functioning yet."))
cancelButton.grid(row=4, column=1, padx=5)
def save(self, title, quantity, price):
conn = sqlite3.connect("ComicEnv.db")
c = conn.cursor()
c.execute("INSERT INTO cdata(unix, datestamp, title, quantity, price) VALUES (?,?,?,?,?)",
(time.time(), date, title, quantity, price))
conn.commit()
conn.close()
我尝试了一些不同的方法,包括:
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(te.get(), qe.get(), pe.get()))
我正在尝试从条目小部件获取用户输入并将其存储在 sqlite3 数据库中。
这是回溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\aedwards\Desktop\deleteme.py", line 106, in <lambda>
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(self.te.get(), self.qe.get(), self.pe.get()))
AttributeError: 'NewProduct' object has no attribute 'te'
非常感谢你们能给我的任何帮助。任何更多信息,请告诉我。
提前致谢!
错误告诉你问题所在:NewProduct
对象没有名为te
的属性。您创建了一个名为 te
的局部变量,但要使其成为属性,您必须创建 self.te
.
此外,您必须在创建小部件的单独语句中调用 grid
,因为 grid(...)
returns None
,因此 te
或self.te
也将是 none。这不仅解决了这个问题,而且使您的代码更易于理解,因为您可以将对 grid
的所有调用放在同一个代码块中,这样您的布局就不会散落在各处。
例如:
def __init__(...):
...
self.te = ttk.Entry(...)
self.qe = ttk.Entry(...)
self.pe = ttk.Entry(...)
...
self.te = grid(...)
self.qe = grid(...)
self.pe = grid(...)
FWIW,我建议不要在这里使用lambda。为您的按钮创建一个适当的函数来调用。它比复杂的 lambda 更容易编写和调试。很少需要在 tkinter 中使用 lambda
。
例如:
def __init__(...):
...
saveButton = ttk.Button(..., command=self.on_save)
...
def on_save(self):
title=self.te.get()
quantity = self.qe.get()
price = self.pe.get()
self.save(title, quantity, price):
我确定这是一个简单的错误,我已将其定位到代码中的特定位置:
class NewProduct(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tLabel = ttk.Label(self, text="Title: ", font=NORM_FONT).grid(row=0, padx=5, pady=5)
qLabel = ttk.Label(self, text="Quantity: ", font=NORM_FONT).grid(row=1, padx=5, pady=5)
pLabel = ttk.Label(self, text="Price: $", font=NORM_FONT).grid(row=2, padx=5, pady=5)
te = ttk.Entry(self).grid(row=0, column=1, padx=5, pady=5) # Add validation in the future
qe = ttk.Entry(self).grid(row=1, column=1, padx=5, pady=5)
pe = ttk.Entry(self).grid(row=2, column=1, padx=5, pady=5)
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(self.te.get(), self.qe.get(), self.pe.get()))
#WHY IS THIS WRONG!!!!!???!?!?!?!?!?
saveButton.grid(row=4, padx=5)
cancelButton = ttk.Button(self, text="Cancel", command=lambda: popupmsg("Not functioning yet."))
cancelButton.grid(row=4, column=1, padx=5)
def save(self, title, quantity, price):
conn = sqlite3.connect("ComicEnv.db")
c = conn.cursor()
c.execute("INSERT INTO cdata(unix, datestamp, title, quantity, price) VALUES (?,?,?,?,?)",
(time.time(), date, title, quantity, price))
conn.commit()
conn.close()
我尝试了一些不同的方法,包括:
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(te.get(), qe.get(), pe.get()))
我正在尝试从条目小部件获取用户输入并将其存储在 sqlite3 数据库中。
这是回溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\aedwards\Desktop\deleteme.py", line 106, in <lambda>
saveButton = ttk.Button(self, text="Save", command=lambda: self.save(self.te.get(), self.qe.get(), self.pe.get()))
AttributeError: 'NewProduct' object has no attribute 'te'
非常感谢你们能给我的任何帮助。任何更多信息,请告诉我。
提前致谢!
错误告诉你问题所在:NewProduct
对象没有名为te
的属性。您创建了一个名为 te
的局部变量,但要使其成为属性,您必须创建 self.te
.
此外,您必须在创建小部件的单独语句中调用 grid
,因为 grid(...)
returns None
,因此 te
或self.te
也将是 none。这不仅解决了这个问题,而且使您的代码更易于理解,因为您可以将对 grid
的所有调用放在同一个代码块中,这样您的布局就不会散落在各处。
例如:
def __init__(...):
...
self.te = ttk.Entry(...)
self.qe = ttk.Entry(...)
self.pe = ttk.Entry(...)
...
self.te = grid(...)
self.qe = grid(...)
self.pe = grid(...)
FWIW,我建议不要在这里使用lambda。为您的按钮创建一个适当的函数来调用。它比复杂的 lambda 更容易编写和调试。很少需要在 tkinter 中使用 lambda
。
例如:
def __init__(...):
...
saveButton = ttk.Button(..., command=self.on_save)
...
def on_save(self):
title=self.te.get()
quantity = self.qe.get()
price = self.pe.get()
self.save(title, quantity, price):