【问题标题】:Frame instance has no __call__ methodFrame 实例没有 __call__ 方法
【发布时间】:2013-02-18 02:51:34
【问题描述】:

当您单击虚线时,我正在尝试将虚线更改为实线。相反,单击一行时出现此错误:

Traceback (most recent call last):
  File "/Users/dan/Documents/pyCatan/path_engine.py", line 106, in <module>
    root.mainloop()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1017, in mainloop
    self.tk.mainloop(n)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1414, in __call__
    self.widget._report_exception()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1175, in _report_exception
    root = self._root()
AttributeError: Frame instance has no __call__ method

类似问题中的常见原因是污染具有多种用途的变量,但我在这里不这样做。另外,我已经声明了正在调用的方法,这是另一个类似问题中的错误。

这是我的新手代码:

from map_gen import MapGen
from gen_board import CatanApp
from Tkinter import *

class PathEngine(object):
    '''Use the configuration options to show or hide certain attributes.'''

    # show the edges explicitly 
    SHOW_EDGES = False 
    # show road intersections as nodes, explicitly
    SHOW_NODES = True
    # color the hexes to their resource color
    COLOR_HEXES = False

    CLICK_ADD_EDGES = True

    # dimensions
    HEIGHT = 600
    WIDTH = 800

    def __init__(self, root):
        self._model = MapGen()
        self._model.gen()
        CatanApp.set_vertices(self._model.get_map())
        self._model.prepare()
        frame = Frame(root, height=PathEngine.HEIGHT, width=PathEngine.WIDTH)
        frame.pack()
        self._canvas = MapDrawer(frame)
        self.render()
        self._canvas.config(height=PathEngine.HEIGHT, width=PathEngine.WIDTH)
        self._canvas.pack()

    def render(self):
        if PathEngine.SHOW_NODES:
            for node in self._model.get_nodes():
                self._canvas.draw_node(*node)
        self.add_edges()

    def add_edges(self):
        for edge in self._model.get_roads():
            if PathEngine.CLICK_ADD_EDGES:
                self._canvas.draw_dashed_edge(edge[0][0], edge[0][1], edge[1][0], edge[1][1])

class MapDrawer(Canvas):
    NODE_RADIUS = 20

    def __init__(self, master):
        Canvas.__init__(self, master)
        self._root = master

    def draw_dashed_edge(self, x1, y1, x2, y2, color=None):
        if color is None:color = "black"
        t = "road_%s_%s" % (str((x1, y1)), str((x2, y2)))
        self.create_line(
            x1,
            y1,
            x2,
            y2,
            fill=color,
            dash=(1, 1),
            width=3,
            tags=("road", t)
        )
        f = lambda event: self.solidify_dashed_edge(t)                  
        self.tag_bind(t, "<Button-1>", f)

    def solidify_dashed_edge(self, tag):
        self.itemconfigure(tag, dash=(0, 1))

    def draw_node(self, x, y, color=None):
        if color is None: color = "white"
        self.create_oval(
            x - MapDrawer.NODE_RADIUS / 2,
            y - MapDrawer.NODE_RADIUS / 2,
            x + MapDrawer.NODE_RADIUS / 2,
            y + MapDrawer.NODE_RADIUS / 2,
            fill=color,
            outline="black"
        )


if __name__ == "__main__":
    root = Tk()
    engine = PathEngine(root)
    root.mainloop()

【问题讨论】:

  • 我认为你的缩进是关闭的——类定义之后的所有函数都是类的一部分吗?
  • 抱歉,我在 Python 中遇到缩进问题,在 StackOverflow 上发帖...
  • 这是一个常见的问题。基本上,您可以完全复制粘贴它,然后点击看起来像 {} 的小按钮,它几乎总是做正确的事情。
  • 这就是我所做的,然后该类不是灰色部分的一部分。
  • 我想我应该说您需要确保在点击该按钮之前选择了所有要缩进的代码:)。无论如何,我为你编辑了。检查以确保我做对了。

标签: python tkinter


【解决方案1】:

您似乎在将属性 _root 添加到您的 Canvas 时遇到了名称冲突:

>>> import Tkinter as tk
>>> a = tk.Canvas()
>>> print a._root
<bound method Canvas._root of <Tkinter.Canvas instance at 0xee1c0>>

这是在没有私有数据的 python 中工作的危险之一 :-)。请注意,_root 是 Canvas 对象的方法。您使用实例属性覆盖该方法:

class MapDrawer(Canvas):
    NODE_RADIUS = 20

    def __init__(self, master):
        Canvas.__init__(self, master)
        self._root = master

其中master 是Frame 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-22
    • 2023-04-03
    • 2012-07-04
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    相关资源
    最近更新 更多