2013-11-03 3 views
0

파이썬 버전 : 2.7.5, 저는 Zelle 's Graphics가 설치되어 있고 다른 기능을 가진 함수에서 비 글로벌 이름을 편집하려고하기 때문에 어떻게해야할지 모릅니다. 다음은 내 코드 예제입니다. 그래서 사용자 정의 함수 "버튼의 내부 button1을 편집 할 수있는 방법은 (가 ...파이썬에서 편집 비 전역 이름

from graphics import * 
import time 
keyPad=GraphWin("Key Pad",300,400) 
def Game(): 
    Buttons() 
    testFor_keyPad_press() 
def Buttons(): 
    button1=Rectangle(Point(1,1),Point(100,100)) 
    button1.setFill('gold') 
    button.draw(keyPad) 
def testFor_keyPad_press(): 
    userInput=keyPad.getMouse() 
    userInputX=str(userInput.getX()) 
    userInputY=str(userInput.getY()) 
    if(userInputX<101 and userInputY<100): 
     button1.setFill('grey') 
     keyPad.update() 
     time.sleep(0.5) 
     button1.setFill('gold') 
     keyPad.update() 
Game() 

내가 그 일을해야한다고 생각하지만, 나에게 전역 이름 '단추 1'이 정의되어 있지 않다는 오류를 제공합니다) "? 아니면 불가능한가요? 이 작업을 수행 여부를 할 수있는 방법이 있다면 알려 주시기 바랍니다 ... 모든 도움을

답변

0
def Buttons(): 
    global button1 
    button1=Rectangle(Point(1,1),Point(100,100)) 
    button1.setFill('gold') 
    button.draw(keyPad) 

함수 외부가 액세스 할 수 있도록 만들 것입니다 ...하지만 정말 당신은 아마 클래스에 로직을 캡슐화한다이다

+0

고마워,하지만 "내 논리를 캡슐에 넣음"이란 뜻을 이해하지 못한다. – NinjaKingRo

관련 문제