繁体   English   中英

Python,实例没有__call__方法

[英]Python, instance has no __call__ method

我正在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

从我所见,人们在覆盖一个值或一个名称遮住另一个值时会遇到此错误。 但是我看不到我可能会因骑行而感到烦恼。 有人可以看到这个问题吗?

driver.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()

animation.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

您要将一个Animation实例传递给after,然后实例将其调用。 我怀疑您是要创建一个实例,然后传入其动画方法,对吗?

编辑:现在,我现在回到Internet来详细说明一下; 问题是此行:

self.root.after(0, animation.Animation(self.root, self.canvas, self.creature))

这将创建一个Animation实例,并将其传递给after方法,然后在无法确定如何调用它时将其炸开。 我会做类似的事情:

animator = animation.Animation(self.root, self.canvas, self.creature)
self.root.after(0, animator.animate) # get things going

另外,正如在其他地方指出的那样,Animation构造函数内部的循环至少以两种方式被破坏:

for i in range(10):
    root.after(250, self.animate())

设置十个与当前时间相关的回调-即在一刻钟后,您将一次触发十个回调。 它们都将失败,因为您要调用animate方法并将其返回值( None )传递给after ,而不是传递方法本身。 换句话说

after(ms, function())

是相同的

value = function()
after(ms, value)

这不是您真正想要的。 解决此问题的一种方法是将self.root = root添加到构造函数,然后执行以下操作:

self.root.after(250, self.animate)

四分之一秒之后,内部animate会触发另一个呼叫。

另外,您可以让Tkinter为您跟踪事情; after方法使您可以将参数传递给回调,并且可以使用该参数来传递after函数本身(!),因此animate方法不必查找它:

def animate(self, after):
    ... do animation ...
    after(250, self.animate, after)

然后通过调用root.after将其关闭,并传入该方法:

root.after(0, animator.animate, root.after)

您将Animation实例作为对self.root.after()回调。 在给定时间后调用回调对象。 调用它们意味着它们的__call__()方法被调用。 您的Animation类没有这种方法。 我建议添加一个然后可以执行您想要的任何操作。 例如:

class Animation():
    #
    # ... your stuff from above ...
    #
    def __call__(self):
        self.animate()

另一种选择是将要调用的方法直接赋予after()

self.root.after(0, animation.Animation(self.root, self.canvas, self.creature).animate)

除了已经提到的故障外,您还有

            for i in range(10):
                root.after(250, self.animate())

在您的animation.py中。 在这里也是如此:您想给root.after()一个可调用的self.animate ,而不是调用一次该可调用的结果( self.animate() )。

()调用前面提到的“事物”。 如果要立即调用,请使用() 如果您不这样做(并且在这种情况下也不这样做),请不要在此处放置()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM