2013-02-18 70 views
0

我試圖在點擊它們時將虛線變成實線。相反,我上線時,點擊此錯誤:在類似的問題框架實例沒有__call__方法

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 

常見的原因是污染具有多種用途的變量,但我沒有這樣做,在這裏。另外,我已經聲明瞭被調用的方法,這是另一個類似問題中的錯誤。

這裏是我的noobie代碼:

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

我認爲你的縮進是關閉的 - 所有的功能都是類定義之後的類嗎? – mgilson 2013-02-18 02:56:50

+0

對不起,我在Python中縮進問題,發佈在StackOverflow上... – BlackSheep 2013-02-18 03:02:16

+0

這是一個常見問題。基本上,您可以將它精確地複製粘貼,然後點擊看起來像「{}」的小按鈕,它總是做正確的事情。 – mgilson 2013-02-18 03:04:43

回答

3

看起來你已經打與添加屬性_rootCanvas名稱衝突:

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

這是工作的危害之一在沒有私人數據的python中:-)。請注意,_rootCanvas對象的一種方法。您覆蓋該方法與實例屬性:

class MapDrawer(Canvas): 
    NODE_RADIUS = 20 

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

masterFrame對象。