2014-11-09 3 views
1

먼저 몇 개의 버튼이있는 창을 만들고 명령을 정의했습니다. while 루프를 추가하여 아무 버튼이 눌러 졌는지 확인한 다음 다음 단계로 이동하기 전까지는 모두 잘 작동합니다. 그러나 그때 창은 나타나지 않고 루프는 영원히 달리고 있습니다. 나는 또한 내 코드에 대한 더 나은 대안이 있는지 알고 싶다.루프 while (파이썬 3)

from tkinter import * 

Round = 0 

def blackC(): 
    global Round 
    print ('0') 
    x = 0 
    Round += 1 

def brownC(): 
    global Round 
    print ('1') 
    x = 1 
    Round +=1 

def redC(): 
    global Round 
    print ('2') 
    x = 2 
    Round += 2 

def win(): 
    window = Tk() 
    window.geometry ('500x500') 
    window.title('HELLO') 
    blackB = Button(text = 'BLACK', command=blackC, width=7, height=3, bd=5) 
    blackB.place(x=1, y=1) 
    brownB = Button(text = 'BROWN', command=brownC, width=7, height=3, bd=5) 
    brownB.place(x=86, y=1) 
    redB = Button(text = 'RED', command=redC, width=7, height=3, bd=5) 
    redB.place(x=172, y=1) 
    window.mainloop() 

while (Round == 0): 
    win() 
while (Round < 3): 
    if (Round == 1): 
     y = x * 10 
     print ('y') 
    elif (Round == 2): 
     y += x 
     print ('y') 

답변

1

정확히 다음 단계로 넘어가는 것이 무슨 뜻인지는 모르겠지만, 당신은 분명히 tkninter의 작동 방식을 오해하고 있습니다. 메인 루프 window.mainloop()에 괄호가 없습니다. mainloop은주기적인 함수이기 때문에 사이클에서 호출하지 않으려 고합니다. 그래서 당신은 그것을 한 번 실행하면 무한히 돌아갑니다. 그래서 코드는 한 번만 함수 win()을 실행해야합니다.

from tkinter import * 

Round=0 

def button(type): 
    global Round 
    print (str(type)) 
    x = type 
    Round += type 

def win(): 
    window = Tk() 
    window.geometry ('500x500') 
    window.title('HELLO') 
    blackB = Button(text = 'BLACK', command=lambda: button(0), width=7, height=3, bd=5) 
    blackB.place(x=1, y=1) 
    brownB = Button(text = 'BROWN', command=lambda: button(1), width=7, height=3, bd=5) 
    brownB.place(x=86, y=1) 
    redB = Button(text = 'RED', command=lambda: button(2), width=7, height=3, bd=5) 
    redB.place(x=172, y=1) 
    window.mainloop 

win() 

당신은 더 나은 코드를 요구했다, 그래서 당신의 버튼을 단지 유형의 parametr을 받아 람다 함수로 호출 한에 대한 FUNC를 다시 작성했다 (보십시오 :. http://www.diveintopython.net/power_of_introspection/lambda_functions.html 을 더 큰 경우 프로젝트는 tkinter window를 클래스로 가지고있는 것이 더 좋지만, 이것으로 충분합니다.

+0

고마워요.하지만 여전히 내 문제를 해결하지 못합니다. –

+0

정말 알아야 할 것이 있습니까? – knezi

+0

변수 값 x와 Round 값을 변경할 수있는 몇 개의 버튼이있는 windoe가 있습니다. 그런 다음 round 값이 특정 숫자에 도달했는지 확인한 다음 while 루프가 있습니다. 단계. 그러나 while 문은 창을 닫을 때까지 실행되지 않습니다. –