问题
I'm writing an inventory database program. I'm a novice so I'm sure I have something wrong.
def select_item():
#Create a Database or Connect to one
conn = sqlite3.connect('inventory.db')
#Create Cursor
c = conn.cursor()
a = id_select.get()
c.execute("SELECT * FROM inventory WHERE oid = " + a)
records = c.fetchall()
for record in records:
Item_editor.insert(0, record[0])
Quantity_editor.insert(0, record[1])
Asset_tag_editor.insert(0, record[2])
Notes_editor.insert(0, record[3])
#Entry Fields
id_select = Entry(editor, width=30)
id_select.grid(row=0, column=1, pady=(20, 0))
Item_editor = Entry(editor, width=30)
Item_editor.grid(row=2, column=1)
Quantity_editor = Entry(editor, width=30)
Quantity_editor.grid(row=3, column=1)
Asset_tag_editor = Entry(editor, width=30)
Asset_tag_editor.grid(row=4, column=1)
Notes_editor = Entry(editor, width=30)
Notes_editor.grid(row=5, column=1)
#Button Time!
id_select = Button(editor, text="Select Item", command=select_item)
id_select.grid(row=1, column=0, columnspan=2, pady=10, padx=10, ipadx=100)
I did not initially have a function, but realized that I would need one for the command in the button. The error points to my variable a = id_select.get(), but I'm pretty sure that I have my entry fields added properly.
回答1:
Your error is from the fact that id_select is a button. You initially had
id_select = Entry(editor, width=30)
which has .get(), but you replaced id_select
in id_select = Button(editor, text="Select Item", command=select_item)
which does not have one. You should name your button something else.
来源:https://stackoverflow.com/questions/60656947/tkinter-python-3-7-attributeerror-button-object-has-no-attribute-get