Search code examples
pythonuser-interfacetkinterscoring

how to create a scoring system


I'm trying to make my scoring system work. it keeps forgetting after I have changed the score once I go back into the class as it is set as 0. How would I change this so the score changes as appropriate?

code:

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()

Solution

  • Try this:

    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()
    

    I moved all of the widgets that are for the question inside a frame. The frame gets destroyed and recreated each time a new question needs to be displayed. I also made sure the user can't just keep clicking on Check Answer after they got the correct answer already (for that question).