Question

I am creating a multiple choice quiz in Tkinter and am using radiobuttons and checkbuttons. For this question I am using radiobuttons. How do I get the value from the radio buttons in order to compare them in the if statement 'Calculate Score'? The computer returns with: 'calculate_score_1() takes exactly 1 argument (0 given)'

Also, how do I pass a variable between classes? I have ten classes for ten questions but want all of them to be able to access the variable 'Score' when adding 1 to the score when the user gets the answer correct.

class Question_1_Window(tk.Toplevel):
'''A simple instruction window'''
def __init__(self, parent):
    tk.Toplevel.__init__(self, parent)
    self.text = tk.Label(self, width=75, height=4, text = "1) Do you have the time to do at least twenty minutes of prefect duty each week?")
    self.text.pack(side="top", fill="both", expand=True)

    question_1_Var = IntVar() #creating a variable to be assigned to the radiobutton

    Yes_1 = Radiobutton(self, text = "Yes", variable = question_1_Var, value=1, height=5, width = 20)
    Yes_1.pack() #creating 'yes' option

    #Here we are assigning values to each option which will be used in the validation

    No_1 = Radiobutton(self, text = "No", variable = question_1_Var, value=2, height=5, width = 20)
    No_1.pack() #creating 'no' option


    def calculate_score_1(self):
        Enter_1.config(state="disabled")
        #self.question_1.config(state="disabled")
        if (question_1_Var.get() == 1) and not (question_1_Var.get() == 2):
            print("calculate score has worked") #test lines
            #score = score + 1
        else:
            print("not worked") #testlines


    Enter_1 = Button(self, text= "Enter", width=10, command = calculate_score_1)
    Enter_1.pack()
Was it helpful?

Solution

calculate_score_1 is not a method of the instance, but is defined inside the __init__ method. Thus, that method should not have the self parameter. Remove that parameter, then it should work. If you need it (you seem not to) you can still use the self parameter of the outer __init__ method.

If you want to access the score from another class (or in fact from another method of the same class) you have to make it a member of the instance, by defining it as self.score = .... You can then access it like this: your_question_1_window_instance.score.

Finally, if you have "ten classes for ten questions" you should try to find some common ground for all those questions and create either a common super class or even one class that can be parametrized to fit all the questions. You just need the title, the type (select one/select many) and a list of answers, and which ones are correct. Everything else -- creating the check boxes, checking the answer, etc. -- should always be the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top