2017-03-08 3 views
0

appJar를 사용하여 GUI를 만들고 버튼을 클릭했는지 확인하려고합니다. 따라서 플래그를 만들려고합니다. 그러지 만, 예상대로 작동하지 않습니다.Python - 버튼을 클릭했는지 확인하기위한 플래그 만들기가 작동하지 않습니다.

시나리오 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() 

글로벌 플래그는 변경되지 않습니다. 나는 내 생각이별로 좋지 않다고 생각하지만 다른 코드를 시도해 보았지만 나를 위해 일하는 것이 아무것도 없었습니다. 어떤 도움이 필요합니까?

+0

'flag'는'anyUpdate'에서 로컬입니다. 언제든지 지정하면 범위에서 전역으로 선언해야합니다. ('전역 플래그'를'anyUpdate' 내부로 옮기십시오.) 자세한 내용은 http://stackoverflow.com/questions/929777/why-does-assigning-to-my-global-variables-not-work-in-python을보십시오. – Gavin

+0

사용자가 '업데이트 사전'을 클릭하지 않고 '바꾸기'를 클릭하면 오류가 발생합니다 (NameError : 전역 이름 '플래그'가 정의되지 않음) '업데이트 사전'이 정의 될 때까지 변수가 정의되지 않습니다. @Gavin –

+0

변경 사항을 제안하면 저에게 도움이됩니다.'someNotImportantCode' 또는'someCode'에'flag'에 할당 한 것이 없으면 당신에게 보여줄 답변을 쓸 것입니다. – Gavin

답변

0

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

그게 좋았어. 지금 당장 두 가지를 모두 시도 했어. 잘 작동 해. 답해줘 고마워. –

관련 문제