python 尝试在Tkinter中按下按钮后取消后使用,但它不起作用

ecbunoof  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(149)

我尝试在tkinter中使用afterafter_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)
5jdjgkvh

5jdjgkvh1#

这是我最后的代码,谢谢你@Bryan奥克利,它工作得很好,在画布上一个字母一个字母地输入问题

from tkinter import *
from quiz_brain import QuizBrain

COLOR = "#375362"
text_map = []
correct_answer = ""
FONT = ("Zippy", 20)
keep = None
press = True

class QuizInterFace:

    def __init__(self, quizz_brain: QuizBrain):
        self.quizz = quizz_brain
        self.text = ""
        self.count = 0
        self.keep = None
        self.window = Tk()
        self.window.title("Quiz App")
        self.window.config(pady=50, padx=50, bg=COLOR)
        self.category_label = Label(text="Category", bg=COLOR, fg="white", font=FONT)
        self.score_label = Label(text="Score", bg=COLOR, fg="white", font=FONT)
        self.canvas = Canvas(height=360, width=400)
        self.question_text = self.canvas.create_text(180, 200, text="Some text", font=("arial", 20, "italic"),
                                                     fill=COLOR, width=270)
        self.category_label.grid(column=0, row=1, columnspan=2)
        self.score_label.grid(column=0, row=0, columnspan=2)
        self.difficulty_label = Label(bg=COLOR, fg="white", font=FONT)
        self.difficulty_label.grid(column=0, row=2, columnspan=2)
        self.canvas.grid(column=0, row=3, columnspan=2, pady=50)
        self.true_image = PhotoImage(file="true.png")
        self.false_image = PhotoImage(file="false.png")
        self.button = IntVar()
        self.true_button = Button(image=self.true_image, highlightthickness=0, activebackground=COLOR,
                                  command=self.true_press, textvariable=self.button)
        self.false_button = Button(image=self.false_image, highlightthickness=0, activebackground=COLOR,
                                   command=self.false_press, textvariable=self.button)
        self.true_button.grid(column=0, row=4)
        self.false_button.grid(column=1, row=4)
        self.get_next_question()
        self.window.mainloop()

    def get_next_question(self):
        global text_map, correct_answer, 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}")
            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.keep = self.window.after(100, self.write)
            else:
                self.count = 0
                self.text = ""
                return
        else:
            self.count = 0
            self.text = ""

            self.window.after_cancel(self.keep)
            self.window.after(100, self.get_next_question)
            return

    def true_press(self):
        global press
        press = False
        self.count = 0
        self.text = ""
        is_right = self.quizz.check_answer("True", correct_answer)
        self.feed_back(is_right)

    def false_press(self):
        global press
        press = False
        self.count = 0
        self.text = ""
        is_right = self.quizz.check_answer("False", correct_answer)
        self.feed_back(is_right)

    def feed_back(self, is_right):
        if is_right:
            self.window.after_cancel(self.keep)
            self.canvas.itemconfig(self.question_text, text="Correct")
            self.window.after(1000, self.get_next_question)
        else:
            self.window.after_cancel(self.keep)
            self.canvas.itemconfig(self.question_text, text="Wrong")
            self.window.after(1000, self.get_next_question)

相关问题