Python Tkinter中的Entry.get()错误

1 投票
1 回答
1708 浏览
提问于 2025-04-18 01:20

我正在尝试在用户输入ID或菜谱名称到输入框时删除一条记录。但是我无法从输入框获取信息,因为出现了以下错误:

"Traceback (most recent call last):
   File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
   return self.func(*args)
  File "H:\A-Level Computing\PROJECT!\Final Code.py", line 368, in delete_recipe
  Retrieve1 = EntryBox1.get()
 AttributeError: 'NoneType' object has no attribute 'get'"

代码如下:

def delete_recipe():

    global EntryBox1, EntryBox2, new_db

    Retrieve1 = EntryBox1.get()
    Retrieve2 = EntryBox2.get()

    new_db = sqlite3.connect('H:\A-Level Computing\PROJECT!\Recipes.db')

    new_db.execute("DELETE from RecipesDetails WHERE Retrieve1 = Rec_ID and Retrieve2 = Name")

    new_db.commit()
    new_db.close()

如果能得到任何帮助,我将非常感激。谢谢!

1 个回答

1

确保你的代码里没有像下面这样的行:

entry = Entry().pack() # entry is None

packgridplace这些方法会返回None,这意味着entry会变成None

你应该把创建Entry和调用packgridplace这几个方法分开写:

entry = Entry()
entry.pack()

撰写回答