我正在开发一个项目,该项目最终将模拟 Twitter 帖子的过滤器。我正在尝试在 Tkinter 中创建一个页面,允许用户输入 Twitter 帐户,然后按下一个按钮,将字符串添加到列表中并清除输入字段(尚未编写追加函数的代码)。代码如下:
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'
我已阅读各种帮助部分,但没有任何效果。大家有解决办法吗?
最佳答案
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)
关于python - 尝试清除 Tkinter Entry Widget 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58983997/