python tkinter应用程序中的奇怪错误

jaxagkaj  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(382)

我正在用python tkinter创建一个问答游戏。代码按预期运行,直到程序完成测验中的所有问题。当它到达那个部分时,它应该显示一个饼图,显示正确和错误的答案。它确实显示饼图,但奇怪的错误会打印到控制台。以下是错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Meirom\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\Meirom\PycharmProjects\quiz\main.py", line 82, in <lambda>
    command=lambda: run2.check_if_correct(correct_answers, counter_int, 3))
  File "C:\Users\Meirom\PycharmProjects\quiz\main.py", line 37, in check_if_correct
    run3.questions()
  File "C:\Users\Meirom\PycharmProjects\quiz\main.py", line 68, in questions
    question_num.configure(text="Question #%d" % (self.counter + 1))
  File "C:\Users\Meirom\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1646, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\Meirom\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label3"

有人能给我解释一下什么需要修理吗?我的代码:

from tkinter import *
from tkinter import messagebox
from itertools import count

window = Tk()
correct_answers = 0
window.title("Python Quiz")
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))

class MyButton(Button):
    def __init__(self, parent,**kwargs):
        super().__init__(parent, kwargs, activebackground="lightblue", font=20)

class App:
    counter = count(0)
    def __init__(self, button1 = MyButton(window), button2 = MyButton(window),
                 button3 = MyButton(window), button4 = MyButton(window)):
            self.button1 = button1
            self.button2 = button2
            self.button3 = button3
            self.button4 = button4

    @staticmethod
    def check_if_correct(correct, counter, num):
        global correct_answers
        if correct[counter] == num:
            messagebox.showinfo("Python Quiz", "Correct!")
            correct_answers += 1
        else:
            messagebox.showinfo("Python Quiz", "Incorrect!")

        run3 = App()
        run3.questions()

    def questions(self):
        self.counter = next(self.counter)
        questions_list = ["What is the data type of the following object?: 3.5",
                          "What is a correct syntax to output \"Hello World\" in Python?",
                          "How do you insert COMMENTS in Python code?",
                          "Which one is NOT a legal variable name?",
                          "How do you create a variable with the NUMERIC value 5?",
                          "What is the output of the following code?",
                          "What is the output of the following code?",
                          "What is the output of the following code?",
                          "What is the output of the following code?"]

        answers = [["float", "int", "str", "bool"],
                   ["printf(\"Hello World\")", "print(Hello World)", "print(\"Hello World\")", "print\"(Hello)\""],
                   ["\\ COMMENT", "// COMMENT", "/* COMMENT */", "# COMMENT"],
                   ["MyVar", "my-var", "_myVar", "my_var"],
                   ["x == 5", "x = int(5)", "x = 5", "Options 2 and 3 are both correct"],
                   [955, 3692, -1, 105],
                   ["The sum is 6", "The sum is 4", "The sum is 5", "The sum is 22"],
                   ["True", "yes", "False", "no"],
                   ["100 97 180 99 101", "100 180 101", "97 99", "None of\nthe above"]]

        correct_answers = [0, 2, 3, 1, 3, 1, 2, 3, 0]
        counter_int = self.counter
        run2 = App()

        if counter_int >= 9:
            run2.win()

        question_num.configure(text="Question #%d" % (self.counter + 1))
        question_num.place(x=380, y=20)
        question_label.configure(text=questions_list[self.counter])
        question_label.place(x=130, y=100)
        self.button1.configure(text=answers[self.counter][0],
                               command=lambda: run2.check_if_correct(correct_answers, counter_int, 0))
        self.button1.place(x=200, y=400)
        self.button2.configure(text=answers[self.counter][1],
                               command=lambda: run2.check_if_correct(correct_answers, counter_int, 1))
        self.button2.place(x=400, y=400)
        self.button3.configure(text=answers[self.counter][2],
                               command=lambda: run2.check_if_correct(correct_answers, counter_int, 2))
        self.button3.place(x=600, y=400)
        self.button4.configure(text=answers[self.counter][3],
                               command=lambda: run2.check_if_correct(correct_answers, counter_int, 3))
        self.button4.place(x=800, y=400)

        if self.counter >= 5:
            question_read = open("question%d.txt" % (self.counter + 1), "r")

            st = ""
            for d in question_read.readlines():
                for a in d:
                    if a not in "{}":
                        st += a

            code_label.configure(text=st)
            question_read.close()
            code_label.place(x=230, y=170)

        return correct_answers[self.counter]

    def win(self):
        destroy_list2 = [code_label, question_num, question_label,
                         self.button1, self.button2, self.button3, self.button4]

        for j in destroy_list2:
            j.destroy()

        data = [["a", (correct_answers / 9), "lime"], ["b", 1 - (correct_answers / 9), "red"]]
        canvas = Canvas(window, height=500, width=500)
        canvas.pack()

        p = 0
        for c in data:
            angle = c[1] * 360
            canvas.create_arc(400, 400, 100, 100, start=p, extent=angle, fill=c[2])
            p += angle

def start_command():
    destroy_list = [welcome, quit_button, start_button]

    for i in destroy_list:
        i.destroy()

    run = App()
    run.questions()

welcome = Label(window, text="Welcome the Python Quiz!", font=("Arial Rounded MT Bold", 50))
welcome.place(x=230, y=20)

start_button = Button(window, text="START", bg="lime", font=("Arial Rounded MT Bold", 50), width=7,
                              command=lambda: start_command())
start_button.place(x=470, y=220)

quit_button = Button(window, text="QUIT", bg="red", font=("Arial Rounded MT Bold", 50), width=7,
                             command=lambda: quit())
quit_button.place(x=470, y=370)
question_label = Label(window, font=("Arial Rounded MT Bold", 30), text="")
question_num = Label(window, font=("Arial Rounded MT Bold", 30), text="")
code_label = Label(window, font=20)

window.mainloop()

程序中使用的文本文件的内容:question6.txt:

def max_num(num1, num2, num3):
  if num1 >= num2 and num1 >= num3:
    print(num1)
  elif num2 >= num1 and num2 >= num3:
    print(num2)
  else:
    print(num3)

max_num(3692,955,105)

问题7.txt:

def count_digit (num):
    sum= 0
    while num>=1:
         sum=sum+num%10
         num=num//10
    print("The sum is: " + str(sum))

count_digit(63625)

问题8.txt:

def is_there_char(str):
    if str.find("!") == -1:
        print("no")
    else:
        print("yes")

is_there_char("pink")

问题9.txt:

my_dict = {
  "32233344": 100,
  "08098509": 97,
  "22222299": 180,
  "23563346" : 99,
  "22224444" : 101
}
for key in my_dict:
    if my_dict [key] >90:
        print(my_dict[key])

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题