在Tkinter中禁用密钥重复

时间:2015-05-04 16:30:12

标签: python tkinter

所以,我试图让球在Tkinter中顺利移动,但关键的重复是弄乱它(即通过过多地调用太快)。有没有办法在一般情况下禁用Tkinter或Python?

这是我的代码:

import Tkinter
import os

root = Tkinter.Tk()

r = 10
x = 150
y = 150

canvas = Tkinter.Canvas(root, width=600, height=600, background='#FFFFFF')
canvas.grid(row=0, rowspan=2, column=1)

circle_item = canvas.create_oval(x-r, y-r, x+r, y+r, 
                                 outline='#000000', fill='#00FFFF')
global leftInt
global upInt
global downInt
global rightInt
leftInt = 0
upInt= 0
downInt = 0
rightInt = 0

def leftMove(Event):
     global leftInt
     leftInt = 1
     ballMove()



def leftStop(Event):
    global leftInt
    global upInt
    global downInt
    global rightInt
    leftInt = 0
    upInt = 0
    downInt = 0
    rightInt = 0
    print("im stop")

def rightMove(Event):
     global rightInt
     rightInt = 1
     gogo = 1
     if (gogo == 1):
         ballMove()
         gogo = 2

def upMove(Event):
     global upInt
     upInt = 1
     gogo = 1
     if (gogo == 1):
         ballMove()
         gogo = 2

def downMove(Event):
     global downInt
     downInt = 1
     gogo = 1
     if (gogo == 1):
         ballMove()
         gogo = 2


def ballMove():
    global leftInt
    global upInt
    global downInt
    global rightInt

    if (rightInt == 1):
        x1, y1, x2, y2 = canvas.coords(circle_item)
        print('im go', x1)
        if x1 < 597:        ## keep it on the canvas
            canvas.move(circle_item, 1, 0)
            root.after(25, ballMove)
        else:
            canvas.move(circle_item, -1, 0)
    if (upInt == 1):
        x1, y1, x2, y2 = canvas.coords(circle_item)
        print('im go', x1)
        if y1 > 3:        ## keep it on the canvas
            canvas.move(circle_item, 0, -1)
            root.after(25, ballMove)
        else:
            canvas.move(circle_item, 0, 1)
    if (downInt == 1):
        x1, y1, x2, y2 = canvas.coords(circle_item)
        print('im go', x1)
        if y1 < 597:        ## keep it on the canvas
            canvas.move(circle_item, 0, 1)
            root.after(25, ballMove)
        else:
            canvas.move(circle_item, 0, -1)
    if (leftInt == 1):
        x1, y1, x2, y2 = canvas.coords(circle_item)
        print('im go', x1)
        if x1 > 3:        ## keep it on the canvas
            canvas.move(circle_item, -1, 0)
            root.after(25, ballMove)
        else:
            canvas.move(circle_item, 1, 0)

ballMove()     


root.bind('<Left>',leftMove)
root.bind('<KeyRelease>',leftStop)
root.bind('<Right>',rightMove)
root.bind('<Up>',upMove)
root.bind('<Down>',downMove)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用Tkinter的after()暂停无限循环一小段时间。一个较短的例子。

HttpClient