Tkinter绑定:循环重复(Python)

时间:2017-09-24 11:01:40

标签: python-2.7 tkinter event-binding

我正在尝试根据箭头键编写一个框在屏幕上移动的游戏,当我按下“空格”按钮时,它会暂停。

出于某种原因,当我按下空格键时,它会返回'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()

2 个答案:

答案 0 :(得分:1)

您需要删除while循环。

您将while循环设置为在True上运行,这当然意味着它将无限期地运行,除非您从循环中断开。您从循环中断开的唯一实例是event.keysym=='space'

因此,如果event.keysym等于space以外的任何内容,则while循环将无限循环。

    def Key(event):
        (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':
            pass

答案 1 :(得分:0)

您希望在遇到event.keysym=='space'条件之前运行该循环。

而不是在True上运行循环并等待space被按下以打破它,您可以尝试将该条件用于while循环,并让它作为只要space尚未被按下。

如下:

    def Key(event):
        while event.keysym!='space':
            (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)