如何创建评分系统

2024-06-26 11:10:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力使我的计分系统工作。当我回到教室时,当我更改分数后,它总是忘记,因为它被设置为0。我将如何改变这一点,以使分数适当改变

代码:

import random
from tkinter import *

class TimesTableGUI:
    
    def __init__(self, parent, score = 0):

        self.score = score
        
        #chooses a random number
        num1 = random.randint(1,10)
        num2 = random.randint(1,10)
        self.answer = num1 * num2

        #making labels
        entry_label = Label(parent, text = str(num1) + " * " + str(num2))
        entry_label.grid(row = 0, column = 0)

        self.e1 = Entry(parent, width = 5)
        self.e1.grid(row = 0, column = 1)

        b1 = Button(parent, text = "Check Answer", command = self.check_answer)
        b1.grid(row = 1, column = 0)

        b2 = Button(parent, text = "Next", command = self.new_question)
        b2.grid(row = 1, column = 1)

        self.b1_output = Label(parent, text = self.score)
        self.b1_output.grid(row = 2, column = 0)

    def check_answer(self): #callback for Check Answer button

        #check if users gets correct answer
        f = int(self.e1.get())
        if f == self.answer:
            self.score +=1
            self.b1_output.configure(text = self.score)
        else:
            self.b1_output.configure(text = self.score)

    def new_question(self): #callback for Next button to next question
        
        self.b1_output.configure(text = " ") 
        radiobuttons = TimesTableGUI(root) #restarts the class for new question
             
if __name__ == "__main__":
    root = Tk()
    root.title("Task 3")
    radiobuttons = TimesTableGUI(root)
    root.mainloop()

Tags: textanswerselfoutputdefcolumnrandomroot
2条回答

问题是,当你调用radiobuttons = TimesTableGUI(root)时,分数也会被重置。
快速解决方案:在new_question中调用radiobuttons = TimesTableGUI(root, score=self.score)时通过评分
更好的解决方案:创建一个重置GUI而不重新初始化整个类的方法

试试这个:

import random
from tkinter import *

class TimesTableGUI:
    def __init__(self, parent, score=0):
        self.score = score
        self.master = parent
        self.ask_new_question()

        self.b1_output = Label(parent, text="Score = %i" % self.score)
        self.b1_output.grid(row=2, column=1)

    def ask_new_question(self):
        self.question_frame = Frame(self.master)
        self.question_frame.grid(row=1, column=1)
        #chooses a random number
        num1 = random.randint(1, 10)
        num2 = random.randint(1, 10)
        self.answer = num1 * num2

        #making labels
        entry_label = Label(self.question_frame, text="%i*%i" % (num1, num2))
        entry_label.grid(row=0, column=0)

        self.e1 = Entry(self.question_frame, width=5)
        self.e1.grid(row=0, column=1)

        self.check_answer_btn = Button(self.question_frame, text="Check Answer",
                                       command=self.check_answer)
        self.check_answer_btn.grid(row=1, column=0)

        b2 = Button(self.question_frame, text="Next",
                    command=self.new_question)
        b2.grid(row=1, column=1)

    def check_answer(self):
        user_answer = int(self.e1.get())
        if user_answer == self.answer:
            self.score += 1
            self.b1_output.configure(text="Score = %i" % self.score)
            # So the user can't submit more than 1 correct answer
            self.check_answer_btn.config(state="disabled")

    def new_question(self):
        # Remove the frame (and all of its children)
        self.question_frame.destroy()
        # Display the new question
        self.ask_new_question()


if __name__ == "__main__":
    root = Tk()
    root.title("Task 3")
    radiobuttons = TimesTableGUI(root)
    root.mainloop()

我在一个框架内移动了所有用于该问题的小部件。每次需要显示新问题时,框架都会被销毁并重新创建。我还确保用户在得到正确答案后(针对该问题),不能一直点击Check Answer

相关问题 更多 >