Python Tkinter动画 [英] Python Tkinter Animation

查看:115
本文介绍了Python Tkinter动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么动画不起作用?运行程序时形状不会移动。

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__(self):
        self.root = Tk()
        self.canvas = Canvas(self.root, width=400, height = 400)
        self.canvas.pack()
        alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',         fill='blue')
        alien2 = self.canvas.create_oval(2, 2, 40, 40, outline='white', fill='red')
        self.canvas.pack()
        self.root.mainloop()

     def animation(self):
        track = 0
        while True:
        x = 5
        y = 0
        if track == 0:
           for i in range(0,51):
                self.time.sleep(0.025)
                self.canvas.move(alien1, x, y)
                self.canvas.move(alien2, x, y)
                self.canvas.update()
           track = 1
           print "check"

        else:
           for i in range(0,51):
                self.time.sleep(0.025)
                self.canvas.move(alien1, -x, y)
                self.canvas.move(alien2, -x, y)
                self.canvas.update()
           track = 0
        print track

alien()


推荐答案

您从不称为 animation 方法。还有其他一些命名问题。

You never called the animation method. There were a couple of other naming issues.

# Assuming Python 2.x
# For Python 3.x support change print -> print(..) and Tkinter to tkinter
from Tkinter import *
import time

class alien(object):
     def __init__(self):
        self.root = Tk()
        self.canvas = Canvas(self.root, width=400, height = 400)
        self.canvas.pack()
        self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',         fill='blue')
        self.alien2 = self.canvas.create_oval(2, 2, 40, 40, outline='white', fill='red')
        self.canvas.pack()
        self.root.after(0, self.animation)
        self.root.mainloop()

     def animation(self):
        track = 0
        while True:
            x = 5
            y = 0
            if track == 0:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, x, y)
                    self.canvas.move(self.alien2, x, y)
                    self.canvas.update()
               track = 1
               print "check"

            else:
               for i in range(0,51):
                    time.sleep(0.025)
                    self.canvas.move(self.alien1, -x, y)
                    self.canvas.move(self.alien2, -x, y)
                    self.canvas.update()
               track = 0
            print track

alien()

这篇关于Python Tkinter动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆