Python - 创建一个标志来检查按钮是否被点击不起作用

Python - Creating a flag to check if a button was clicked is not working

我正在使用 appJar 创建一个 GUI,我正在尝试检查按钮是否被单击,所以我想创建一个标志来执行此操作,但是它没有像我预期的那样工作。我希望程序执行以下操作:

场景 1:
1- 加载字典
2- 'Replace' 单击按钮 > 词典未更新

场景 2:
1- 加载字典
2- 单击 'Update dictionary' 按钮 > 将标志设置为 'yes'
3- 'Replace' 单击按钮> 重新加载 字典

我的代码:

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

如您所见,全局标志没有改变。我觉得我的想法不是很好,但我尝试了不同的代码,但对我没有任何用处,我感到被卡住了。有帮助吗?

python 中的 global 关键字非常容易使用,只要你记得如果你想在某个内部范围内(即在函数内)分配给全局变量,你需要在该范围内声明变量全局。

[关于全局变量的更多 Whosebug 链接:Using global variables in a function other than the one that created them and Use of "global" keyword in Python]

所以 - 为了让您的代码正常工作,您可以将其更改为:

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

我会做一些其他更改,但至少要使您的代码更清晰。

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