5

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

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()
4

3 回答 3

21

您的animation方法中有一个while True永不中断的循环。这在 GUI 程序中是一个禁忌,因为从不返回,它会阻止 GUI 的事件循环处理事件。因此,例如,如果您有一个菜单,那么用户将无法选择任何菜单项。animation除了您在方法中实施的任何操作外,GUI 将显示为冻结状态。

这是对@Tim 代码的轻微修改,它通过删除while循环并在返回前简单地移动外星人一步来解决这个问题。self.master.after在方法结束时调用,animation让事件循环在短暂的暂停后再次调用动画。


import tkinter as tk
import time

class Alien(object):
    def __init__(self, canvas, *args, **kwargs):
        self.canvas = canvas
        self.id = canvas.create_oval(*args, **kwargs)
        self.vx = 5
        self.vy = 0

    def move(self):
        x1, y1, x2, y2 = self.canvas.bbox(self.id)
        if x2 > 400:
            self.vx = -5
        if x1 < 0:
            self.vx = 5
        self.canvas.move(self.id, self.vx, self.vy)

class App(object):
    def __init__(self, master, **kwargs):
        self.master = master
        self.canvas = tk.Canvas(self.master, width=400, height=400)
        self.canvas.pack()
        self.aliens = [
            Alien(self.canvas, 20, 260, 120, 360,
                  outline='white', fill='blue'),
            Alien(self.canvas, 2, 2, 40, 40, outline='white', fill='red'),
        ]
        self.canvas.pack()
        self.master.after(0, self.animation)

    def animation(self):
        for alien in self.aliens:
            alien.move()
        self.master.after(12, self.animation)

root = tk.Tk()
app = App(root)
root.mainloop()
于 2012-11-04T02:35:21.153 回答
6

您从未调用过该animation方法。还有其他几个命名问题。

# 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()
于 2012-11-04T01:36:53.677 回答
3

这是一种使用循环的方法:

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

可更新变量是在更新时保持不变并且可以再次更新的变量。

于 2017-01-17T05:49:48.513 回答