我正在开发一个项目,该项目最终将模拟Twitter帖子的过滤器。我正在尝试在Tkinter中创建一个页面,该页面将允许用户输入Twitter帐户,然后按一个将字符串添加到列表中并清除输入字段的按钮(尚未对append函数进行编码)。代码如下:
def Add():
F.title('Twitter Filter: Add to Filter')
def h_delete():
Entry.delete(h,first=0,last=END) # should clear entry, instead returns NoneType error
for widget in F.winfo_children():
widget.destroy() # clears widgets of previous window
global a1
a1=tk.StringVar() # declares a variable that will be used to append a list with the text in the Entry
h=tk.Entry(F,textvariable=a1).grid(row=1,column=1) # creates the entry I want cleared
EntryButton=tk.Button(F,text='Add this account',command=h_delete).grid(row=2,column=1) # initiates the entry clearing function
BackButton=tk.Button(F,text='Back to Home',command=Home).grid(row=3,column=1) # returns to home screen
但是,当我运行代码时,收到如下的NoneType错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/skor8427/Desktop/Twitter Filter/TwitterFilter.py", line 22, in h_delete
Entry.delete(h,first=0,last=END) # should clear entry, instead returns NoneType error
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2519, in delete
self.tk.call(self._w, 'delete', first, last)
AttributeError: 'NoneType' object has no attribute 'tk'
我已经阅读了各种帮助部分,但没有任何帮助。有人有解决方案吗?
答案 0 :(得分:1)
h = tk.Entry(F, textvariable=a1)
h.grid(row=1, column=1)
您必须在其他行中网格h,否则它将变为NoneType 尝试使用此代码段代替
h = tk.Entry(F, textvariable=a1).grid(row=1, column=1)