2015-05-04 24 views
1

因此,我試圖讓Tkinter中的球順利移動,但關鍵的重複是搞砸了它(即通過調用太快而使其太快)。有沒有辦法在一般情況下禁用Tkinter或Python?禁用Tkinter中的重複鍵

這是我的代碼:

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() 

回答

0

使用的Tkinter的()後暫停無限循環的時間很短。一個簡短的例子。

from Tkinter import * 

class BounceTest(): 
    def __init__(self): 
     self.running=True 
     self.window = Tk() 
     canvas = Canvas(self.window, width = 400, height = 300) 
     canvas.grid() 

     x0 = 10 
     y0 = 50 
     x1 = 60 
     y1 = 100 
     i = 0 
     deltax = 2 
     deltay = 3 
     which = canvas.create_oval(x0,y0,x1,y1,fill="red", tag='red_ball') 

     Button(text="Stop", bg='yellow', command=self.stop_it).grid(row=1) 

     while self.running: 
      canvas.move('red_ball', deltax, deltay) 
      canvas.after(20) ## give it time to draw everything 
      canvas.update() 
      if x1 >= 400: 
       deltax = -2 
      if x0 < 0: 
       deltax = 2 
      if y1 > 290: 
       deltay = -3 
      if y0 < 0: 
       deltay = 3 
      x0 += deltax 
      x1 += deltax 
      y0 += deltay 
      y1 += deltay 
     self.window.mainloop() 

    def stop_it(self): 
     self.running=False 

BounceTest()