【问题标题】:how to create a scoring system如何创建评分系统
【发布时间】:2023-03-10 05:58:01
【问题描述】:

我正在努力使我的评分系统正常工作。回到班级后,我更改分数后它一直忘记,因为它设置为 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()

【问题讨论】:

  • 这个:radiobuttons = TimesTableGUI(root) #restarts the class for new question 是个坏主意。
  • 你会建议做什么?

标签: python user-interface tkinter scoring


【解决方案1】:

试试这个:

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(针对那个问题)。

【讨论】:

    【解决方案2】:

    问题是,当您拨打radiobuttons = TimesTableGUI(root) 时,分数也会被重置。
    快速解决方案:当您在@ 中拨打radiobuttons = TimesTableGUI(root, score=self.score) 时通过分数987654323@
    更好的解决方案:创建一个无需重新初始化整个类即可重置 GUI 的方法。

    【讨论】:

    • 这将继续在屏幕上创建新的小部件,而不会破坏旧的。
    • 这在 cmets 中完全提到了。另外,您的“快速解决方案”将提供NameError。
    • 我真的不知道你为什么给我一个负面评价。我说这是一个快速的解决方案而不是一个好的解决方案,我在它下面放了一个更好的解决方案。另外,我对其进行了测试,但没有引发 NameError。 Plus Plus 我在 cmets 中看不到它
    • 从同一个文件运行时可能不会出错,但无论如何,self 不会在类之外定义。这不是负面评价,而是诚实的评价。
    • 总的来说,我对诚实或负面评价没有任何问题,我只是不理解批评。现在我明白了,谢谢!
    猜你喜欢
    • 2014-07-12
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多