【问题标题】:Python - Creating a flag to check if a button was clicked is not workingPython - 创建一个标志来检查按钮是否被点击不起作用
【发布时间】:2017-03-08 13:11:33
【问题描述】:

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

场景 1:
1- 加载字典
2-单击“替换”按钮>字典未更新

场景 2:
1- 加载字典
2- 单击“更新字典”按钮 > 将标志设置为“是”
3- 点击“替换”按钮 > 重新加载字典

我的代码:

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

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

【问题讨论】:

  • flaganyUpdate 中是本地的。如果您在任何时候分配给它,您需要在一个范围内声明它是全局的。 (将global flag 移至anyUpdate 内部。查看stackoverflow.com/questions/929777/… 了解更多详情
  • 我试过了,如果用户在第一次没有点击“更新字典”的情况下点击“替换”会报错(NameError: global name 'flag' is not defined)导致变量不是在单击“更新字典”之前定义。 @加文
  • 如果我提出更改建议,它对我有用,所以除非你在someNotImportantCodesomeCode 中有分配给flag 的东西,否则你应该没问题。我会写一个答案给你看。

标签: python flags


【解决方案1】:

python 中的global 关键字很容易使用,只要您记住,如果要在某个内部范围(即在函数内)分配全局变量,则需要在该范围内声明变量 global .

[有关全局变量的更多 StackOverflow 链接:Using global variables in a function other than the one that created themUse 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')

【讨论】:

  • 太好了,我现在明白了,我尝试了两种方法,它们都很好用。谢谢你的回答。
猜你喜欢
  • 2021-03-08
  • 2013-05-09
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多