我的节目是一个定时添加测验。它工作正常,但在输入错误答案(command = submit_answer)后,此错误消息仍然出现。这是完整的错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Maggie\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\Maggie\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 596, in callit
func(*args)
File "C:\Users\Maggie\Documents\python\prototype2.py", line 115, in countdown
self.label.configure(text="%d" % self.remaining)
File "C:\Users\Maggie\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1330, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\Maggie\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".54870736.54870896.54871216.54871728"
这是我程序的代码:
class Add(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, background="blue")
button = tk.Button(self, text="Main Menu", font=LARGE_FONT, background="white",
command=lambda: controller.show_frame(Main))
button.place(relx=0.83, rely=0.92)
self.button4 = tk.Button(self, text="Start Game", font=LARGE_FONT, background="white",
command=self.time_start)
self.button4.place(relx=0.4, rely=0.8, relheight=0.1, relwidth=0.2)
self.after_id = None
def time_start(self):
self.button4.destroy()
self.x = int(random.uniform(1,10))
self.y = int(random.uniform(50,100))
self.z = int(random.uniform(10,50))
print(self.x+self.y+self.z)
self.qlabel = tk.Label(self, text= (self.x,"+",self.y,"+",self.z,"=",), font=WORD_FONT, bg="blue")
self.qlabel.place(relx=0.25, rely=0.3, relheight=0.05, relwidth=0.3)
self.e1 = tk.Entry(self)
self.e1.place(relx=0.5, rely=0.3, relheight=0.05, relwidth=0.2)
self.ebutton = tk.Button(self, text="Done", font=TITLE_FONT, background="white",
command=self.submit_answer)
self.ebutton.place(relx=0.35, rely=0.5, relheight=0.2, relwidth=0.3)
self.label = tk.Label(self, text="", width=10, font=LARGE_FONT, bg="white")
self.label.place(relx=0.1, rely=0.1)
self.remaining = 0
self.countdown(10)
def submit_answer(self):
a = (self.x+self.y+self.z)
if int(self.e1.get()) == a:
answerlabel = tk.Label(self, text="Correct!", font=LARGE_FONT, background="blue")
answerlabel.place(relx=0.42, rely=0.1, relheight=0.1, relwidth=0.2)
self.label.destroy()
self.qlabel.destroy()
self.e1.destroy()
self.ebutton.destroy()
self.after_cancel(self.after_id)
self.time_start()
else:
answerlabel = tk.Label(self, text="Game Over.", font=LARGE_FONT, background="blue")
answerlabel.place(relx=0.42, rely=0.1, relheight=0.1, relwidth=0.2)
self.ebutton.destroy()
self.label.destroy()
self.restartbutton = tk.Button(self, text="Reset Game", font=LARGE_FONT, background="white",
command= self.reset_game)
self.restartbutton.place(relx=0.65, rely=0.92)
def countdown(self, remaining = None):
if remaining is not None:
self.remaining = remaining
if self.remaining <= 0:
self.after_id = None
self.label.configure(text="Time's up!")
self.ebutton.destroy()
else:
self.label.configure(text="%d" % self.remaining)
self.remaining = self.remaining - 1
self.after_id = self.after(1000, self.countdown)
def reset_game(self):
self.button4 = tk.Button(self, text="Start Game", font=LARGE_FONT, background="white",
command=self.time_start)
self.button4.place(relx=0.4, rely=0.8, relheight=0.1, relwidth=0.2)
self.label.destroy()
self.qlabel.destroy()
self.e1.destroy()
self.ebutton.destroy()
blanklabel = tk.Label(self, background="blue")
blanklabel.place(relx=0.42, rely=0.1, relheight=0.1, relwidth=0.2)
self.restartbutton.destroy()
答案 0 :(得分:3)
以after
开头的作业试图在该窗口小部件被销毁后修改窗口小部件。