首页 > 解决方案 > 尝试清除 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'

我已经阅读了各种帮助部分,但没有任何效果。有人有解决方案吗?

标签: pythontkinter

解决方案


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) 

推荐阅读