当我编写代码时,我偶然发现了这个错误:
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.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'
有修复吗?
答案 0 :(得分: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
已自行调用。