我在 python2.7 中使用 tkinter。我想要做的就是在一个类中绘制一个画布,然后在另一个类中绘制一个将在画布周围移动一个正方形的类。但由于某种原因,我得到
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 495, in callit
func(*args)
AttributeError: Animation instance has no __call__ method
据我所见,当人们覆盖了一个值,或者一个名字遮蔽了另一个值时,他们会得到这个错误。但我看不出我可能会过度骑行。其他人可以看到问题吗?
驱动程序.py:
from Tkinter import *
import animation
class Alien(object):
def __init__(self):
#Set up canvas
self.root = Tk()
self.canvas = Canvas(self.root, width=400, height=400)
self.canvas.pack()
#Vars
self.map = [[1, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 1, 0]]
self.x = 0
self.y = 0
r = 50
land = {}
#Draw Init
for i, row in enumerate(self.map):
for j, cell in enumerate(row):
color = "black" if cell else "green"
land[(i, j)] = self.canvas.create_rectangle(r * i, r * j , r * (i + 1), r * (j + 1),
outline=color, fill=color)
self.creature = self.canvas.create_rectangle(r * self.x, r * self.y, r * (self.x + 1), r * (self.y + 1),
outline="red", fill="red")
self.canvas.pack(fill=BOTH, expand=1)
#Action
self.root.after(0, animation.Animation(self.root, self.canvas, self.creature))
#Clost TK
self.root.mainloop()
a = Alien()
动画.py:
from random import randrange
class Animation():
def __init__(self, root, canvas, creature):
self.x = self.y = 0
i = randrange(1, 5)
if i == 1:
self.y = -1
elif i == 2:
self.y = 1
elif i == 3:
self.x = -1
elif i == 4:
self.x = 1
self.canvas = canvas
self.creature = creature
for i in range(10):
root.after(250, self.animate())
def animate(self):
#root.after(250, self.animate(canvas, creature))
"""Moves creature around canvas"""
self.canvas.move(self.creature, self.x * 50, self.y * 50)
#self.canvas.update() no longer needed