如何解决清空输入框时的属性错误
我在写代码的时候遇到了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\Bryan\Desktop\Python overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
我已经用过搜索功能,但似乎没有解决我的问题。我想要实现的是清空一个输入框。
# Maak een grafische interface / Create a graphical interface
gui = Tk()
gui.resizable(width=FALSE, height=FALSE)
# Verklaringen / Definitions
def verify():
overify = antwoord.get()
if overify == Nederlandsevertaling:
ctypes.windll.user32.MessageBoxA(0, "Goed gedaan", "werkend", 1)
oentry.delete(0,END)
else:
ctypefs.windll.user32.MessageBoxA(0, "Slecht gedaan", "Werkend", 1)
oentry.delete()
antwoord = StringVar()
willekeurig = random.randint(0,9)
# Laad de database / Load the database
cnx = mysql.connector.connect(user='-', password='-', host='-', database='Overhoor')
cursor = cnx.cursor()
query = "SELECT FRwoord, NLwoord FROM unite8app1 WHERE id=%s"
cursor.execute(query, (willekeurig,))
for (FRwoord, NLwoord) in cursor:
Fransevertaling = FRwoord
Nederlandsevertaling = NLwoord
# Uiterlijk van het venster / Appearance of the window
gui.configure(background="white")
gui.title("Overhoorprogramma - Bryan")
# Grafische objecten / Graphical objects
style = ttk.Style()
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
# Stop de grafische interface
gui.mainloop()
oentry = ttk.Entry(gui,textvariable=antwoord, font=("Times", 18)).grid(row=1, column=0,padx=5, pady=5)
obutton = ttk.Button(gui,text="Suivant", command = verify).grid(row=1, column=1,padx=5, pady=5)
如果你按下obutton,应该能清空oentry里的文本。为此我尝试使用:
oentry.delete(0,END)
还有几种不同的.delete方法。但它并没有清空文本框,反而给我返回了一个错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\Bryan\Desktop\Python overhoorprogramma.py", line 18, in verify
oentry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'
有没有解决办法?
2 个回答
0
我之前在实际“创建”文本框之前写了删除和插入的语句,像这样:
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
把创建的部分放在前面就解决了这个问题:
my_text = tk.Text(frame2, width=40, height=10, font=("Helvetica", 18))
my_text.pack(pady=25, padx=25)
my_text.delete(1.0, tk.END)
my_text.insert(tk.INSERT, title)
2
每个Tkinter控件的grid
方法都是就地操作的,也就是说它总是返回None
。
换句话说,像下面这样的写法:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white").grid(row=0, column=0,padx=5, pady=5, sticky="W")
应该改成这样:
olabel = ttk.Label(gui,text=Fransevertaling,font=("Times", 18), background="white")
olabel.grid(row=0, column=0,padx=5, pady=5, sticky="W")
也就是说,grid
方法应该单独写在一行。