python - 尝试清除 Tkinter 条目小部件时出错
问题描述
我正在开发一个项目,该项目最终将模拟 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'
我已经阅读了各种帮助部分,但没有任何效果。有人有解决方案吗?
解决方案
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)
推荐阅读
- android - 查找电子邮件和/或用户名是否存在于 Firebase - Android
- css - css - 损坏文本的样式元素(空白:正常)
- discord.py - Discord.py 如何检查任务是否处于活动状态?
- mongodb - 当 collection_filters 设置为将当天的数据从 DocumentDB/MongoDB 同步到文件/ElasticSearch 时,Compose Transporter 抛出错误
- swift - 将 UIView 作为子视图添加到 UIButton 并将其置于前面无法正常工作
- state-machine - 构造一个接受语言 L = {w | 的 DFA w ∈ {a,b}* 和 Na(w) mod 3 > Nb (w) mod 3}
- python - 我正在学习套接字,但无法解决我的客户端文件中的错误
- c - 以高效的方式用 C 编写序列
- reactjs - 未显示使用调试值
- java - 合并后Lombok的Gradle编译错误