2013-02-18 4 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 

일반적인 원인은 여러 용도로 변수를 오염했다,하지만 난 여기에 일을하고 있지 않다 대신, 한 줄에 클릭 할 때 나는이 오류가 발생합니다. 또한 나는 호출되는 메서드를 선언했다.이 메서드는 다른 유사한 오류의 오류이다. 당신이 당신의 Canvas에 속성 _root를 추가로 이름 충돌을 칠 것 같은

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

+0

죄송합니다, StackOverflow에 게시하는 파이썬의 들여 쓰기 문제가 있습니다 ... – BlackSheep

+0

일반적인 문제입니다. 기본적으로, 정확히 복사해서 붙여 넣기 만하면 {{}'처럼 보이는 작은 버튼을 누르기 만하면됩니다. – mgilson

답변

3

것 같습니다 : 여기

내 noobie 코드

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

그것은 작업의 위험 요소 중 하나입니다 개인 데이터가없는 파이썬에서는 :-). _rootCanvas 개체의 메서드입니다. 당신은 인스턴스 속성과 메소드를 오버라이드 (override) :

class MapDrawer(Canvas): 
    NODE_RADIUS = 20 

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

masterFrame 객체입니다.

관련 문제