禁用Tkin中的键重复

2024-10-04 11:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我试着让球在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()

Tags: moveifdefrootitemglobalcanvasx1
1条回答
网友
1楼 · 发布于 2024-10-04 11:34:49

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

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

相关问题 更多 >