Tkinter python 3.7 AttributeError: 'Button' object has no attribute 'get'

Tkinter python 3.7 AttributeError: 'Button' object has no attribute 'get'

我正在编写库存数据库程序。我是新手,所以我确定我有问题。

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)

我最初没有功能,但意识到按钮中的命令需要一个功能。错误指向我的变量 a = id_select.get(),但我很确定我的输入字段已正确添加。

您的错误是因为 id_select 是一个按钮。你最初有 id_select = Entry(editor, width=30) 有 .get(),但是你在 id_select = Button(editor, text="Select Item", command=select_item) 中替换了 id_select 而没有。您应该将按钮命名为其他名称。