0

I created a small program that shows a window and asks passwords, ids, to check if the user is saved in a database. If password is correct, then it affects True to a boolean named mdp_valide ('password_is_valid' in english) which was False before, and destroys the window of connexion. It's a function that changes that value, so I used a global statement at the top of the function. However, when it closes the window, the value mdp_valide is back to False Here's some code to help you understand

First, the main program that will call the other function:

while 1:
    mdp_valide, utilisateur_en_cours = fenetre_connection(False, None)
    print ('in the main', mdp_valide, utilisateur_en_cours)
    if not mdp_valide:
        sys.exit()
    else :
        lancer_messagerie(utilisateur_en_cours)

Then, the function which is not working:

def fenetre_connection (mdp_val, utilisateur):
    mdp_valide = mdp_val
    utilisateur_en_cours = utilisateur

    root_co = Tk ()

    # .... Lots of stuff

    def verification():
        mdp_co = mot_de_passe.get()
        global mdp_valide
        global utilisateur_en_cours

        if mdp_co == recuperer_donnee_utilisateur (identifiant_utilisateur_co, 'mot_de_passe'): # check if the password is the one of the database

            print ('condition checked')
            mdp_valide = True
            utilisateur_en_cours = identifiant_utilisateur_co
            print ("before destroying : ", mdp_valide, utilisateur_en_cours)
            root_co.destroy()
            print ("after destroying : ", mdp_valide, utilisateur_en_cours)
        else:
            return 1

    Button(Frameboutons_co, text="Valider", font='Cambria', command = verification).pack(side=RIGHT) #Bouton qui verifie la validité du pseudo et du mot de passe


    root_co.mainloop()

    print ('before return : ', mdp_valide)

    return mdp_valide

Test :

before destroying:  True guil23
after destroying :  True guil23
before return :  False None
in the main : False None

The problem is here : the function verification () does change the value of mdp_valide into True, but after returning the value, it's back to False

5
  • 2
    I don't see global mdp_valide in the function fenetre_connection. That might be your problem. Commented Apr 10, 2017 at 15:50
  • Er I removed it, sorry. But it was in the original post. I put it back Commented Apr 10, 2017 at 16:20
  • Actually, it was already there, but you might habe been mistaken by the wrong indentation. I corrected it Commented Apr 10, 2017 at 16:27
  • Here is a shorter version that illustrate the problem: repl.it/HDdW Commented Apr 10, 2017 at 16:30
  • This is related: stackoverflow.com/questions/13099908/… Commented Apr 10, 2017 at 16:35

1 Answer 1

2

The problem is that in fenetre_connection, mdp_valide is not global. So it is a local variable. Then in verification, you use global mdp_valide, so that one is a global variable.

When verification finishes, you return the local variable from fenetre_connection.

In python 3 you can use nonlocal: https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement

In python 2, you can declare mdp_valide as global in fenetre_connection so that both variables are in the global scope, and therefore are the same

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, it seems to be working again. (I used the global because the nonlocal was a bit over complicated :) ).
But still, I don't understand how is that possible. Is not global supposed to take the variable which is somewhere "above" ?
@Guil23 - global does not mean "somewhere" above, but specifically at the module level.
As I had understood, when you have : [Spacename1 : somevariable, [Spacename2]], and you want to access somevariable within Spacename2, using global gives you that access
@Guil23 in your case, you had: a different mdp_valide in global and in fenetre_connection (local) (that would be connexion, btw). In verification, you are using the global one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.