Home > Net >  Why I can't increment my variable in this tkinter program
Why I can't increment my variable in this tkinter program

Time:11-17

I try to make a programme to improve mental calculation with an gui with tkinter. And I apologies in advance if I my errors are really stupid, but it's the first time I use tkinter.

So I have the function addition which gives me two numbers (a,b) and the sum of these two number (c). The user enters the result in the entry, but the problem is that when I try to verify if the result is the good one, I want to add 1 at the general score if it's the good answer in the function getEntry.

(By the way, I don't know if it's a good this to do the verification in the same function that I get the entry. I will be much easier for me to use a return at the end of the function and then use an other one but if I use a return in my function I don't know how to receive the value that I return in a variable because I call the function thanks to the button...)

I need to increment the variable for the score but I can't and I don't why...

I use the built-in function global for the function score because it's not in the function getEntry(). But when I click on the button to verify my answer, I have this error message

line 96, in getEntry
    score= score  1
NameError: name 'score' is not defined

I test with a really basic program:

x=0
def fonc():
    global x
    x=x 1
fonc()
print(x)

And it print 1 So I don't understand why it doesn't work in my big program...

Here is my code:

class PageAdd1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        self.id = controller.id

        lblTitle = tk.Label(self, text="Mode Addition Facile") 
        lblTitle.pack()

        btnBack = tk.Button(self, text="Revenir à la sélection de la difficulté",
                            command=lambda: controller.up_frame("PageAdd"))
        btnBack.pack()
        score=0

        def getEntry():
            global score
            res=int(entryRes.get())
            if res == c:
                score= score  1
            print(score)

        a, b, c = addition(1)
        lstQuestion = ["Quelle est le résultat de l'oppération ", str(a), "   ", str(b), "?"]
        question = "".join(lstQuestion)
        lblQuestion = tk.Label(self, text=question)
        lblQuestion.pack()

        res=tk.IntVar()
        entryRes= tk.Entry(self, textvariable= res)
        entryRes.pack()

        btnValid = tk.Button(self, text= "Valider",command=getEntry)
        btnValid.pack()

Thank you in advance for your help!!

CodePudding user response:

score=0 is a local variable in __init__. You need to add global score to __init__ as well:

class PageAdd1(tk.Frame):
    def __init__(self, parent, controller):
        # ...
        global score
        score=0

        def getEntry():
            global score
            res=int(entryRes.get())
            if res == c:
                score= score  1
            print(score)

Alternatively, since you have a class, you could make everything members of the class:

class PageAdd1(tk.Frame):
    def __init__(self, parent, controller):
        # ...
        self.score=0
        # ...
        btnValid = tk.Button(self, text= "Valider",command=self.getEntry)
        btnValid.pack()

    def getEntry(self):
        res=int(entryRes.get())
        if res == c:
            self.score= self.score  1
        print(self.score)
  • Related