我尝试在tkinter中使用after
和after_cancel
方法来显示画布中的文本。
它正常工作,但如果用户按下一个按钮之前,在画布上的文本写作是compelete它显示错误的信息[和scaping显示一些数据在画布上]。
这是我的代码,我没有得到任何错误,但它不工作得很好。
def get_next_question(self):
global text_map, correct_answer, keep, press
press = True
if self.quizz.still_remaining_question():
self.canvas.config(bg="white")
question = self.quizz.next_question()
list_name = list(question)
text_map = list_name
text_map = list_name[2]
category = list_name[0]
difficulty = list_name[1]
correct_answer = list_name[4]
self.category_label.config(text=f"Category : {category.title()}")
self.difficulty_label.config(text=f"Difficulty : {difficulty.title()}")
self.score_label.config(text=f"Score : {self.quizz.score} / {self.quizz.question_number - 1}")
keep = self.write()
else:
self.canvas.itemconfig(self.question_text, text=f"You rich the end of the Quizz your final Score is:"
f"{self.quizz.score}/{self.quizz.question_number}")
self.true_button.config(state="disabled")
self.false_button.config(state="disabled")
def write(self):
if press:
self.canvas.itemconfig(self.question_text, text=self.text)
if len(self.text) != len(text_map):
self.text += text_map[self.count]
self.count += 1
self.window.after(100, self.write)
else:
self.count = 0
self.text = ""
return
else:
self.count = 0
self.text = ""
try:
self.window.after_cancel(self.write)
except ValueError:
pass
finally:
self.window.after(100, self.get_next_question)
return
def true_press(self):
global press
press = False
is_right = self.quizz.check_answer("True", correct_answer)
self.feed_back(is_right)
def false_press(self):
global press
press = False
is_right = self.quizz.check_answer("False", correct_answer)
self.feed_back(is_right)
def feed_back(self, is_right):
if is_right:
self.canvas.itemconfig(self.question_text, text="Correct")
else:
self.canvas.itemconfig(self.question_text, text="Wrong")
try:
self.window.after_cancel(self.write)
except ValueError:
pass
finally:
self.window.after(1000, self.get_next_question)
1条答案
按热度按时间5jdjgkvh1#
这是我最后的代码,谢谢你@Bryan奥克利,它工作得很好,在画布上一个字母一个字母地输入问题