화살표 키에 따라 상자가 화면에서 움직이는 게임을 작성하려고합니다. '스페이스'버튼을 누르면 일시 중지됩니다.Tkinter 바인딩 : 루프 반복 자체 (파이썬)
아무 이유없이, 스페이스 버튼을 누르면 아무것도 발생하지 않은 것처럼 'NewGame()'루프로 돌아갑니다. 왜 이런 일이 일어나는 걸까요?
from Tkinter import *
HEIGHT = 400
WIDTH = 300
cHEIGHT=HEIGHT-100
cWIDTH=WIDTH
TOPLEFT=3
BUTTOMRIGHT=13
RECTANGLE_SIDE=BUTTOMRIGHT-TOPLEFT
def NewGame():
def Key(event):
while True:
(x1,y1,x2,y2)=canvas.coords(head)
if event.keysym =='Right':
canvas.move(head,1,0)
root.update()
if x1>=cWIDTH:
canvas.move(head, -cWIDTH,0)
elif event.keysym=='Left':
canvas.move(head,-1,0)
root.update()
if x2<=0:
canvas.move(head, cWIDTH,0)
elif event.keysym=='Up':
canvas.move(head,0,-1)
root.update()
if y2<=0:
canvas.move(head, 0,cHEIGHT)
elif event.keysym=='Down':
canvas.move(head,0,1)
root.update()
if y1>=cHEIGHT:
canvas.move(head, 0,-cHEIGHT)
elif event.keysym=='space':
break
canvas.delete("all")
head=canvas.create_rectangle(TOPLEFT,TOPLEFT,BUTTOMRIGHT,BUTTOMRIGHT)
root.bind('<Key>', Key)
root = Tk()
root.geometry(('%dx%d')%(HEIGHT,WIDTH))
b1 = Button(root, text = 'New Game', command=NewGame)
b1.pack()
canvas=Canvas(root, height = cHEIGHT, width = cWIDTH)
canvas.pack()
root.mainloop()
이, 도움이되지 않는 다른 키를 누를 때까지 움직 만들 수 있습니다. 이것이 while while : while 루프를 사용한 이유입니다. 내 프로그램을 실행할 때 'space'가 눌려지면 while 루프가 종료되지 않습니다. –