首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Tkinter)如何在放置对象后运行动画?

(Tkinter)如何在放置对象后运行动画?
EN

Stack Overflow用户
提问于 2020-12-09 03:55:53
回答 1查看 47关注 0票数 1

我正在努力学习tkinter,我找到了一个球的动画(in this website)。

我试着把它修改成一个可以用鼠标放置的球。到目前为止,我已经完成了第一部分,但我不知道如何添加运动。我推测画布需要在动画代码之前运行,以获得球坐标。

代码语言:javascript
复制
import tkinter 
import time


animation_window_width=800
animation_window_height=600
animation_ball_radius = 30
animation_ball_min_movement = 5
animation_refresh_seconds = 0.01

def animate_ball(window, canvas, xinc, yinc, ball):

    while True:
        canvas.move(ball,xinc,yinc)
        window.update()
        time.sleep(animation_refresh_seconds)
        ball_pos = canvas.coords(ball)
        xl,yl,xr,yr = ball_pos
        if xl < abs(xinc) or xr > animation_window_width-abs(xinc):
            xinc = -xinc
        if yl < abs(yinc) or yr > animation_window_height-abs(yinc):
            yinc = -yinc


def position_ball(event): 
    ball = canvas.create_oval(event.x-animation_ball_radius,
               event.y-animation_ball_radius,
               event.x+animation_ball_radius,
               event.y+animation_ball_radius,
               fill="blue", outline="white", width=4)
    
window = tkinter.Tk()
window.title("Tkinter Animation Demo")
window.geometry(f'{animation_window_width}x{animation_window_height}')
canvas = tkinter.Canvas(window)
canvas.configure(bg="black")
canvas.pack(fill="both", expand=True)
ball=canvas.bind("<Button-1>", position_ball)
ball 

#animate_ball(window, canvas, animation_ball_min_movement, animation_ball_min_movement, ball)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-09 04:28:47

这是一个非常糟糕的例子。对于大多数GUI,您永远不应该在GUI线程中使用带有sleep()的无限循环,因为它会阻塞GUI主循环(pygame是一个值得注意的例外)。大多数GUI,包括tkinter,都是“事件驱动的”,你需要使用事件来做事情。我想你想要的事件是鼠标移动。事件将包含鼠标的x,y位置,所以剩下的就是将其转移到球上。

代码语言:javascript
复制
import tkinter

animation_window_width=800
animation_window_height=600
animation_ball_radius = 30
animation_ball_min_movement = 5
animation_refresh_seconds = 0.01

def place_ball(event):
    canvas.unbind("<Motion>") # stop responding to motion
    canvas.xinc = animation_ball_min_movement
    canvas.yinc = animation_ball_min_movement
    animate_ball()

def locate_ball(event):
    canvas.coords(ball,
        event.x-animation_ball_radius,
        event.y-animation_ball_radius,
        event.x+animation_ball_radius,
        event.y+animation_ball_radius)

# Create and animate ball in an infinite loop
def animate_ball(event=None):
    canvas.move(ball,canvas.xinc,canvas.yinc) # move the ball
    xl,yl,xr,yr = canvas.coords(ball) # get current coordinates

    if xl < abs(canvas.xinc) or xr > animation_window_width-abs(canvas.xinc):
      canvas.xinc = -canvas.xinc
    if yl < abs(canvas.yinc) or yr > animation_window_height-abs(canvas.yinc):
      canvas.yinc = -canvas.yinc

    canvas.coords(ball, xl,yl,xr,yr) # set new coordinates

    canvas.after(20, animate_ball) # set the loop event

window = tkinter.Tk()
window.title("Tkinter Animation Demo")
window.geometry(f'{animation_window_width}x{animation_window_height}')
canvas = tkinter.Canvas(window)
canvas.configure(bg="black")
canvas.pack(fill="both", expand=True)
ball = canvas.create_oval(0,0,0,0,fill="blue", outline="white", width=4)
canvas.bind('<Motion>', locate_ball)
canvas.bind('<Button-1>', place_ball)
window.mainloop()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65206001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档