0

I'm using appJar to create a GUI and I'm trying to check if a button was clicked or not, so I thought of creating a flag to do that, however it does not work as I expected. I want the program to do the following:

Scenario 1:
1- Load dictionary
2- 'Replace' button clicked > dictionary is not updated

Scenario 2:
1- Load dictionary
2- 'Update dictionary' button is clicked > set flag to 'yes'
3- 'Replace' button clicked > reload dictionary

My code:

# defined flag globally
global flag
flag = 'no'

# Function to change the global var -flag-
def anyUpdate(f):
    if f == 'yes':
        flag = f # change global variable -flag- to 'yes'
        return True
    else:
        return False

def press(btn):
    # Edit dictionary clicked
    if btn=="Update dictionary":
        anyUpdate('yes') # call function - 'yes' sent

    # Replace clicked
    if btn=="Replace":
        if someNotImportantCode:
            someCode
        else:
            print flag # prints 'no' even if update dictionary clicked means global var didn't change
            checkUpdate = anyUpdate(flag) # call function - global var sent
            print checkUpdate # prints False of course since flag is 'no'
            if checkUpdate == True:
                # reload dictionary
                reps = getDictionary()

As you see the global flag doesn't change. I feel my idea isn't very good, but I've tried different codes nothing is working for me and I feel stuck. Any help?

  • `flag` is local in `anyUpdate`. You need to declare it global in a scope if you assign to it at any point. (move `global flag` to inside `anyUpdate`. look at http://stackoverflow.com/questions/929777/why-does-assigning-to-my-global-variables-not-work-in-python for more detail – Gavin Mar 08 '17 at 13:17
  • I tried that, it gives an error if the user clicks 'Replace' without clicking 'Update dictionary' for the first time (NameError: global name 'flag' is not defined) cause the variable is not defined until 'Update dictionary' is clicked. @Gavin –  Mar 08 '17 at 13:30
  • It works for me if I make the change suggested, so unless you have something in `someNotImportantCode` or `someCode` that assigns to `flag` you should be fine. I'll write up an answer to show you. – Gavin Mar 08 '17 at 23:44

1 Answers1

0

The global keyword in python is pretty easy to use as long as you remember that if you want to assign to a global variable in some inner scope (ie within a function) you need to declare the variable global in that scope.

[ More StackOverflow links on globals: Using global variables in a function other than the one that created them and Use of "global" keyword in Python ]

So - to get your code working you could change it to:

# defined flag globally
flag = 'no'
someNotImportantCode = 0
someCode = 'What is happening here'

# Function to change the global var -flag-
def anyUpdate(f):
    global flag  # Define flag to be a global (module level) variable
    if f == 'yes':
        flag = f # change global variable -flag- to 'yes'
        return True
    else:
        return False

def press(btn):
    # Edit dictionary clicked
    if btn=="Update dictionary":
        anyUpdate('yes') # call function - 'yes' sent

    # Replace clicked
    if btn=="Replace":
        if someNotImportantCode:
            someCode
        else:
            print flag # prints 'no' even if update dictionary clicked means global var didn't change
            checkUpdate = anyUpdate(flag) # call function - global var sent
            print checkUpdate # prints False of course since flag is 'no'
            if checkUpdate == True:
                # reload dictionary
                reps = getDictionary()

I would make several other changes though at the very least to make your code clearer.

from __future__ import print_function  # Future proof your code for python3
# Defined flag at module level
# Also don't store yes/no and then translate to True/False later
update_clicked = False
# Only used to stop my test code bugging out
someNotImportantCode = 0
someCode = 'What is happening here'


def press(btn):
    # Edit dictionary clicked
    global update_clicked
    if btn == "Update dictionary":
        update_clicked = True
        return

    # Replace clicked
    if btn == "Replace":
        if someNotImportantCode:
            someCode
        else:
            # Don't check whether var is equal to True, either use:
            #   if var: , or
            #   if var is True:
            if update_clicked:
                print('Dictionary reloaded')
                reps = getDictionary()
            else:
                print('Dictionary not updated')
Community
  • 1
  • 1
Gavin
  • 1,070
  • 18
  • 24
  • That's great I get it now, I tried both ways they all work well. Thank you for your answer. –  Mar 09 '17 at 02:57