简体   繁体   English

按键按下事件不会更新 while 循环内的变量

[英]Key down event does not update variable inside while loop

I'm currently making an animation on the canvas using tkinter with python and running into some issues.我目前正在使用 tkinter 和 python 在画布上制作动画并遇到一些问题。 The relevant portion of my code is below.我的代码的相关部分如下。

k = 0
dirX, dirY = 1, 0

def LeftButton(event):
    dirX = -abs(dirX)

c.bind("<Left>", LeftButton)

while 1:
    k %= 6
    #show one frame of animation and hide the rest
    c.itemconfig(pc[k], state=tk.NORMAL)
    c.itemconfig(pc[(k+1) % 6], state=tk.HIDDEN)
    c.itemconfig(pc[(k+2) % 6], state=tk.HIDDEN)
    c.itemconfig(pc[(k+3) % 6], state=tk.HIDDEN)
    c.itemconfig(pc[(k+4) % 6], state=tk.HIDDEN)
    c.itemconfig(pc[(k+5) % 6], state=tk.HIDDEN)
    #move all of the frames to the same location
    c.move(pc[k],dirX*5,dirY*5)
    c.move(pc[(k+1) % 6],dirX*5,dirY*5)
    c.move(pc[(k+2) % 6],dirX*5,dirY*5)
    c.move(pc[(k+3) % 6],dirX*5,dirY*5)
    c.move(pc[(k+4) % 6],dirX*5,dirY*5)
    c.move(pc[(k+5) % 6],dirX*5,dirY*5)
    
    k += 1
    #update the canvas
    c.update()
    time.sleep(.075)

Basically, pc is a numpy array of image objects on the canvas and at each index is a different image frame of the animation.基本上, pc是画布上图像对象的 numpy 数组,每个索引处是动画的不同图像帧。 Every .075 seconds it shows one frame and hides all of the others, and then increments the position of all of them.每 0.075 秒显示一帧并隐藏所有其他帧,然后增加所有帧的位置。 I am trying to build it so that when I press the left key, dirX becomes negative so my animation goes to the left.我正在尝试构建它,以便当我按下左键时, dirX变为负数,因此我的动画向左移动。 The animation is working fine, but the problem is that anything outside of the while loop is fixed, ie pressing a button does nothing.动画工作正常,但问题是 while 循环之外的任何内容都是固定的,即按下按钮什么也不做。 It may be that I am binding them wrong, but I binded it to a mouse click which I have done before and it still didn't work.可能是我绑定错误,但我将它绑定到我以前做过的鼠标点击,但它仍然不起作用。 Is there a better way to animate than a while loop?有没有比 while 循环更好的动画方式?
One way I considered is instead of having while 1: I can put while not (code for keydown event): , but I don't know the code for a keydown event and I just want the variable dirX to update and then reinitiate the while loop, which is also problematic.我考虑的一种方法是代替while 1: I can put while not (code for keydown event): ,但我不知道 keydown 事件的代码,我只希望变量dirX更新然后重新启动 while循环,这也是有问题的。 What is the best way I can fix this?我能解决这个问题的最好方法是什么? Thank you谢谢
Edit编辑
I ran a test to see if LeftButton would print anything if the event was triggered, and it didn't.我运行了一个测试,看看如果触发事件, LeftButton是否会打印任何内容,但它没有。 I think this means that while the code is running through the while loop it isn't checking or handling events.我认为这意味着当代码在 while 循环中运行时,它不会检查或处理事件。

In a GUI program, you already have the mainloop and any additional infinitely running loop will block the application.在 GUI 程序中,您已经有了主mainloop ,任何额外的无限运行循环都会阻塞应用程序。
Instead, try using the after function of tkinter.相反,请尝试使用 tkinter 的after函数。

def animation():
    # your itemconfig, move commands and
    # other animation logic.
    c.after(75, animation)

# start animation
animation()

This will tell tkinter to continue with the mainloop, and after 75ms, call the animation function again.这将告诉 tkinter 继续主循环,并在 75 毫秒后再次调用动画函数。 after is often called from the root widget, but your canvas widget should work, too. after通常从root小部件调用,但您的画布小部件也应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM