TKinter:按下键时移动,未按下时停止
TKinter: Move while key is pressed, stopped when not
我正在尝试用我的球创造一个 "smoother" 运动。基本上,我希望我的程序检测何时按下某个键,并让它以恒定速度移动球,当未按下该键时,让它停止。
这是我的代码:
import time
import Tkinter
import math
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
leftInt = 0
def leftMove(Event):
global leftInt
leftInt = 1
gogo = 1
if (gogo == 1):
ballMove()
gogo = 2
def leftStop(Event):
global leftInt
leftInt = 0
print("im stop")
def rightMove(Event):
canvas.move(circle_item, 5, 0)
x1, y1, x2, y2 = canvas.coords(circle_item)
def upMove(Event):
canvas.move(circle_item, 0, -5)
x1, y1, x2, y2 = canvas.coords(circle_item)
def downMove(Event):
canvas.move(circle_item, 0, 5)
x1, y1, x2, y2 = canvas.coords(circle_item)
def ballMove():
global leftInt
while (leftInt == 1):
print('im go')
canvas.move(circle_item, -5, 0)
x1, y1, x2, y2 = canvas.coords(circle_item)
time.sleep(.1)
ballMove()
root.bind('<Left>', leftMove)
root.bind('<KeyRelease>', leftStop)
root.bind('<Right>', rightMove)
root.bind('<Up>', upMove)
root.bind('<Down>', downMove)
root.mainloop()
我试图在按下时创建一个 while 循环,然后让 KeyRelease 停止它。为什么不停?我该如何解决这个问题?
请注意,OS 的重复率及其处理按下的键的方式决定了发生的情况,因此您可能会同时看到 "im go" 和 "im stop" 即使您连续按住该键。更改 ballMove() 以使用 Tkinter 的 after 方法。您不应在 GUI 程序中使用 time.sleep(),因为它会中断 GUI 的无限循环。
def ballMove():
global leftInt
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, -5, 0)
root.after(100, ballMove)
我正在尝试用我的球创造一个 "smoother" 运动。基本上,我希望我的程序检测何时按下某个键,并让它以恒定速度移动球,当未按下该键时,让它停止。
这是我的代码:
import time
import Tkinter
import math
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
leftInt = 0
def leftMove(Event):
global leftInt
leftInt = 1
gogo = 1
if (gogo == 1):
ballMove()
gogo = 2
def leftStop(Event):
global leftInt
leftInt = 0
print("im stop")
def rightMove(Event):
canvas.move(circle_item, 5, 0)
x1, y1, x2, y2 = canvas.coords(circle_item)
def upMove(Event):
canvas.move(circle_item, 0, -5)
x1, y1, x2, y2 = canvas.coords(circle_item)
def downMove(Event):
canvas.move(circle_item, 0, 5)
x1, y1, x2, y2 = canvas.coords(circle_item)
def ballMove():
global leftInt
while (leftInt == 1):
print('im go')
canvas.move(circle_item, -5, 0)
x1, y1, x2, y2 = canvas.coords(circle_item)
time.sleep(.1)
ballMove()
root.bind('<Left>', leftMove)
root.bind('<KeyRelease>', leftStop)
root.bind('<Right>', rightMove)
root.bind('<Up>', upMove)
root.bind('<Down>', downMove)
root.mainloop()
我试图在按下时创建一个 while 循环,然后让 KeyRelease 停止它。为什么不停?我该如何解决这个问题?
请注意,OS 的重复率及其处理按下的键的方式决定了发生的情况,因此您可能会同时看到 "im go" 和 "im stop" 即使您连续按住该键。更改 ballMove() 以使用 Tkinter 的 after 方法。您不应在 GUI 程序中使用 time.sleep(),因为它会中断 GUI 的无限循环。
def ballMove():
global leftInt
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, -5, 0)
root.after(100, ballMove)