Python Tkinter Animation

后端 未结 3 933
猫巷女王i
猫巷女王i 2021-02-04 21:04

Why is the animation not working? The shape doesn\'t move when I run the program.

from Tkinter import *
import time



class alien(object):
     def __init__(sel         


        
3条回答
  •  深忆病人
    2021-02-04 21:58

    Here's a way of doing it using a loop:

    from tkinter import * # version 3.x
    
    tk = Tk()
    
    frame = Frame(tk)
    canvas = Canvas(frame) # use canvas
    
    frame.pack(fill = BOTH, expand = 1)
    canvas.pack(fill = BOTH, expand = 1)
    
    ball = canvas.create_oval(10, 10, 30, 30, tags = 'ball') # create object to animate
    
    def animation(x_move, y_move):
        canvas.move(ball, x_move, y_move) # movement
        canvas.update()
        canvas.after(20) # milliseconds in wait time, this is 50 fps
    
        tk.after_idle(animation, x_move, y_move) # loop variables and animation, these are updatable variables
    
    animation(2, 2) # run animation
    

    An updatable variable is a variable that stays the same when updated and can be updated again.

提交回复
热议问题