破線をクリックすると、破線を塗りつぶした線に変更しようとしています。代わりに、行をクリックすると次のエラーが表示されます。
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()