【发布时间】:2014-08-21 15:57:00
【问题描述】:
这是一个非常基本的程序,我想用它制作两个移动的球,但实际上只有一个球会移动。
我也尝试了一些变化,但无法让第二个球移动;另一个相关问题 - 有些人使用move(object) 方法来实现这一点,而其他人则使用delete(object) 然后重绘它。我应该使用哪一个?为什么?
这是我的代码,它只动画/移动一个球:
from Tkinter import *
class Ball:
def __init__(self, canvas, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.canvas = canvas
self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, fill="red")
def move_ball(self):
while True:
self.canvas.move(self.ball, 2, 1)
self.canvas.after(20)
self.canvas.update()
# initialize root Window and canvas
root = Tk()
root.title("Balls")
root.resizable(False,False)
canvas = Canvas(root, width = 300, height = 300)
canvas.pack()
# create two ball objects and animate them
ball1 = Ball(canvas, 10, 10, 30, 30)
ball2 = Ball(canvas, 60, 60, 80, 80)
ball1.move_ball()
ball2.move_ball()
root.mainloop()
【问题讨论】:
-
请修正意图!