我敢肯定,这是一个简单的错误,我已经将其定位于在代码中的特定点:如何让Entry Widget传入函数?
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()))
我m试图从入口小部件获取用户输入并将其存储在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'
你们可以给我任何帮助将不胜感激。更多信息,请让我知道。
在此先感谢!
1.当分配类的属性,执行'self.te = whatever',不'TE = whatever'。 2.不要在同一行上分配和打包。请参阅http://stackoverflow.com/questions/1101750/python-tkinter-attributeerror-nonetype-object-has-no-attribute-get – Kevin 2015-04-01 16:02:20
难道是这么简单吗?我现在就试一试。 – Silight 2015-04-01 16:11:35
这样做。非常感谢。 我甚至没有想过看网格部分。所有的研究和这是因为我问了错误的问题。 再次感谢! – Silight 2015-04-01 16:21:23