python 3.x - How do I get the Entry Widget to pass into a function? -
i'm sure simple mistake , i've localized specific spot in code:
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 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 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 cdata(unix, datestamp, title, quantity, price) values (?,?,?,?,?)", (time.time(), date, title, quantity, price)) conn.commit() conn.close()
i've tried few different things including: savebutton = ttk.button(self, text="save", command=lambda: self.save(te.get(), qe.get(), pe.get()))
i'm trying user input entry widget , store in sqlite3 database.
this traceback:
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'
any guys give me appreciated. more information, please let me know.
thanks in advance!
the error telling problem: newproduct
object has no attribute named te
. have created local variable named te
, attribute must create self.te
.
also, must call grid
in separate statement create widget, because grid(...)
returns none
, te
or self.te
none. not solves problem, makes code easier understand, because can put of calls grid
in same block of code don't have layout scattered on place.
for example:
def __init__(...): ... self.te = ttk.entry(...) self.qe = ttk.entry(...) self.pe = ttk.entry(...) ... self.te = grid(...) self.qe = grid(...) self.pe = grid(...)
fwiw, recommend not using lambda here. create proper function button call. it's easier write , debug complicated lambda. there's need use lambda
in tkinter.
for example:
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):
Comments
Post a Comment