Daniel Kats
Daniel Kats

Reputation: 5554

Frame instance has no __call__ method

I am trying to change dashed lines into filled lines when you click on them. Instead, I get this error when clicking on a line:

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

Common causes in similar questions were polluting variables with multiple uses, but I am not doing that here. Also, I have declared the method that is being called, which was the error in another, similar question.

Here is my noobie code:

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

Upvotes: 0

Views: 1686

Answers (1)

mgilson
mgilson

Reputation: 309841

It looks like you've hit a name conflict with adding an attribute _root to your Canvas:

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

It's one of the hazards of working in python where there is no private data :-). Notice that _root is a method of Canvas objects. You override that method with an instance attribute:

class MapDrawer(Canvas):
    NODE_RADIUS = 20

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

Where master is a Frame object.

Upvotes: 3

Related Questions