def plot_results(self): grid_x = (self.canvas_width - 2 * self.canvas_margin) / (self.grid_size) grid_y = (self.canvas_height - 2 * self.canvas_margin) / (self.grid_size) pin_dx = grid_x / 6 pin_dy = grid_y / 6 master = Tk() w = Canvas(master, width=self.canvas_width, height=self.canvas_height) w.pack() for node in self.graph: i, j = self.id_to_coord(node) if node[-1] == 's': i += 0.5 j += 0.5 nx, ny = self.get_xy(i, j) if node[-1] == 's': fill_col = 'green' w.create_rectangle(nx - pin_dx, ny - pin_dy, nx + pin_dx, ny + pin_dy, fill=fill_col) else: fill_col = 'black' w.create_oval(nx - 2, ny - 2, nx + 2, ny + 2, fill=fill_col) self.draw_routes(w) w.update() w.postscript(file='sol35.ps', colormode='color') mainloop()
class DiscreteTAMPViewer(object): def __init__(self, rows, cols, width=500, height=250, side=25, block_buffer=10, title='Grid', background='tan', draw_fingers=False): assert (rows <= MAX_ROWS) assert (cols <= MAX_COLS) tk = Tk() tk.withdraw() top = Toplevel(tk) top.wm_title(title) top.protocol('WM_DELETE_WINDOW', top.destroy) self.width = width self.height = height self.rows = rows self.cols = cols self.canvas = Canvas(top, width=self.width, height=self.height, background=background) self.canvas.pack() self.side = side self.block_buffer = block_buffer self.draw_fingers = draw_fingers self.cells = {} self.robot = [] self.draw_environment() def transform_r(self, r): return self.table_y1 + r * (self.side + 2 * self.block_buffer ) + 2 * self.block_buffer + self.side / 2 def transform_c(self, c): # assert r >= 0 and r < self.width return self.table_x1 + c * (self.side + 2 * self.block_buffer ) + 2 * self.block_buffer + self.side / 2 def draw_environment(self, table_color='lightgrey', bin_color='grey'): table_width = self.cols * ( self.side + 2 * self.block_buffer) + 2 * self.block_buffer table_height = self.rows * ( self.side + 2 * self.block_buffer) + 2 * self.block_buffer border_buffer = 50 #self.table_x1 = border_buffer self.table_y1 = self.height - table_height - border_buffer self.table_x1 = self.width / 2 - table_width / 2 #self.table_y1 = self.height/2-table_height/2 bin_width = 20 self.environment = [ self.canvas.create_rectangle(self.table_x1, self.table_y1, self.table_x1 + table_width, self.table_y1 + table_height, fill=table_color, outline='black', width=2), self.canvas.create_rectangle(self.table_x1 - bin_width, self.table_y1, self.table_x1, self.table_y1 + table_height, fill=bin_color, outline='black', width=2), self.canvas.create_rectangle(self.table_x1 + table_width, self.table_y1, self.table_x1 + table_width + bin_width, self.table_y1 + table_height, fill=bin_color, outline='black', width=2), self.canvas.create_rectangle(self.table_x1, self.table_y1 + table_height, self.table_x1 + table_width, self.table_y1 + table_height + bin_width, fill=bin_color, outline='black', width=2), self.canvas.create_rectangle( self.table_x1 - bin_width, self.table_y1 + table_height, self.table_x1 + table_width + bin_width, self.table_y1 + table_height + bin_width, fill=bin_color, outline='black', width=2), ] pose_radius = 2 for r in range(self.rows): for c in range(self.cols): x = self.transform_c(c) y = self.transform_r(r) self.environment.append( self.canvas.create_oval(x - pose_radius, y - pose_radius, x + pose_radius, y + pose_radius, fill='black')) def draw_robot(self, r, c, color='yellow'): # TODO - could also visualize as top grasps instead of side grasps grasp_buffer = 3 # 0 | 3 | 5 finger_length = self.side + grasp_buffer # + self.block_buffer finger_width = 10 gripper_length = 20 if self.draw_fingers: gripper_width = self.side + 2 * self.block_buffer + finger_width else: gripper_width = self.side stem_length = 50 stem_width = 20 x = self.transform_c(c) y = self.transform_r( r) - self.side / 2 - gripper_length / 2 - grasp_buffer finger_x = gripper_width / 2 - finger_width / 2 self.robot = [ self.canvas.create_rectangle(x - stem_width / 2., y - stem_length, x + stem_width / 2., y, fill=color, outline='black', width=2), self.canvas.create_rectangle(x - gripper_width / 2., y - gripper_length / 2., x + gripper_width / 2., y + gripper_length / 2., fill=color, outline='black', width=2), ] if self.draw_fingers: self.robot += [ self.canvas.create_rectangle(x + finger_x - finger_width / 2., y, x + finger_x + finger_width / 2., y + finger_length, fill=color, outline='black', width=2), self.canvas.create_rectangle(x - finger_x - finger_width / 2., y, x - finger_x + finger_width / 2., y + finger_length, fill=color, outline='black', width=2), ] self.canvas.update() def draw_block(self, r, c, name='', color='red'): x = self.transform_c(c) y = self.transform_r(r) self.cells[(x, y)] = [ self.canvas.create_rectangle(x - self.side / 2., y - self.side / 2., x + self.side / 2., y + self.side / 2., fill=color, outline='black', width=2), self.canvas.create_text(x, y, text=name), ] # def delete(self, (x, y)): # if (x, y) in self.cells: # self.canvas.delete(self.cells[(x, y)]) def clear(self): self.canvas.delete('all') def save(self, filename): self.canvas.postscript(file='%s.ps' % filename, colormode='color') from PIL import ImageGrab ImageGrab.grab((0, 0, self.width, self.height)).save(filename + '.jpg')
def initUI(self, tree, scaling): self.parent.title("MST") self.pack(fill=BOTH, expand=1) canvas = Canvas(self, width=10000, height=10000) #canvas.create_oval(10, 10, 15, 15, outline="red", fill="red", width=1) tempMax1 = 0 tempMax2 = 0 for node in tree.nodes: tempMax1 = max(tempMax1, node.GetLoc()[0]) tempMax2 = max(tempMax2, node.GetLoc()[1]) for x in range(tempMax1 + 1): for y in range(tempMax2 + 1): canvas.create_oval(scaling * (x + 1) - 1, scaling * (tempMax2 - y + 1) - 1, scaling * (x + 1) + 1, scaling * (tempMax2 - y + 1) + 1, outline="black", fill="black", width=1) for edge in tree.edges: point1 = edge.GetPoints()[0] point2 = edge.GetPoints()[1] point3 = edge.GetPoints()[2] canvas.create_oval(scaling * (point1[0] + 1) - 2, scaling * (tempMax2 - point1[1] + 1) - 2, scaling * (point1[0] + 1) + 2, scaling * (tempMax2 - point1[1] + 1) + 2, outline="red", fill="red", width=1) canvas.create_oval(scaling * (point2[0] + 1) - 2, scaling * (tempMax2 - point2[1] + 1) - 2, scaling * (point2[0] + 1) + 2, scaling * (tempMax2 - point2[1] + 1) + 2, outline="red", fill="red", width=1) if point3 != None: point3 = edge.GetPoints()[2] canvas.create_line(scaling * (point1[0] + 1), scaling * (tempMax2 - point1[1] + 1), scaling * (point3[0] + 1), scaling * (tempMax2 - point3[1] + 1), fill="red") canvas.create_line(scaling * (point2[0] + 1), scaling * (tempMax2 - point2[1] + 1), scaling * (point3[0] + 1), scaling * (tempMax2 - point3[1] + 1), fill="red") else: canvas.create_line(scaling * (point1[0] + 1), scaling * (tempMax2 - point1[1] + 1), scaling * (point2[0] + 1), scaling * (tempMax2 - point2[1] + 1), fill="red") canvas.pack(fill=BOTH, expand=1) canvas.update() canvas.postscript(file="graph.6.ps", colormode='color')
class SurfaceManipulator(Frame): r""" A translation surface editor in tk. """ # STATIC METHODS AND OBJECTS current = None # boolean variable to remember if the hook was run! _clear_hook_was_run = 0 @staticmethod def launch(geometry="800x700+10+10"): r"""Prefered way to gain access to a SurfaceManipulator window.""" if SurfaceManipulator.current is None: SurfaceManipulator._clear_hook() root = Tk() root.geometry(geometry) SurfaceManipulator.current = SurfaceManipulator(root) return SurfaceManipulator.current @staticmethod def _window_destroyed(surface_manipulator): if SurfaceManipulator.current is surface_manipulator: SurfaceManipulator.current = None @staticmethod def _clear_hook(): if not SurfaceManipulator._clear_hook_was_run: # Hack due to Nathan Dunfield (http://trac.sagemath.org/ticket/15152) import IPython.lib.inputhook as ih ih.clear_inputhook() SurfaceManipulator._clear_hook_was_run = 1 # NORMAL METHODS def __init__(self, parent, surface=None, surfaces=[]): r""" INPUT: - ``surfaces`` -- a list of surfaces that the editor may modify - ``surface`` -- surface selected by default - ``parent`` -- parent Tk window """ Frame.__init__(self, parent) self._parent = parent self.pack(fill="both", expand=1) # Run something when closing self._parent.wm_protocol("WM_DELETE_WINDOW", self.exit) # Surface currently being manipulated self._surface = None # List of surfaces in editor self._surfaces = [] # More variables to initialize self._currentActor = None # Initialization of GUI self._init_menu() self._init_gui() # Setup surface list for s in surfaces: self.add_surface(s) # Setup initial surface if surface is not None: self.add_surface(surface) self.set_surface(surface) def __repr__(self): return "Surface manipulator" def add_mega_wollmilchsau(self): from geometry.mega_wollmilchsau import MegaWollmilchsau s = MegaWollmilchsau() sm, sb = s.get_bundle() self.set_surface(sb) def add_octagon(self): from geometry.similarity_surface_generators import TranslationSurfaceGenerators ss = TranslationSurfaceGenerators.regular_octagon() ss.edit() def _init_menu(self): self._menubar = Menu(self._parent) menubar = self._menubar self._parent.config(menu=menubar) #new_menu = Menu(menubar, tearoff=0) #new_menu.add_command(label="Billiard Table", command=self.on_new_similarity_surface) file_menu = Menu(menubar, tearoff=0) #file_menu.add_cascade(label="New", menu=new_menu) file_menu.add_command(label="Octagon", command=self.add_octagon) file_menu.add_command(label="MegaWollmilchsau", command=self.add_mega_wollmilchsau) file_menu.add_separator() file_menu.add_command(label="About", command=self.on_about) file_menu.add_command(label="Export PostScript", command=self.on_export) file_menu.add_command(label="Exit", command=self.exit, accelerator="Alt+F4") menubar.add_cascade(label="File", underline=0, menu=file_menu) self._surface_menu = Menu(menubar, tearoff=0) self._selected_surface = IntVar() self._selected_surface.set(-1) menubar.add_cascade(label="Surface", underline=0, menu=self._surface_menu) self._surface_menu.add_radiobutton(label="None", command=self.menu_select_surface, variable=self._selected_surface, value=-1) def _init_gui(self): self._parent.title("FlatSurf Editor") self._canvas = Canvas(self, bg="#444", width=300, height=200) self._canvas.pack(fill="both", expand=1) self.bottom_text = Label(self, text="Welcome to FlatSurf.", anchor="w") self.bottom_text.pack(fill="x", expand=0) self.set_actor(None) def add_surface(self, newsurface): r""" Add a surface to the display list for the window. Returns the index of the new surface in the surface list. """ if (newsurface == None): return -1 i = 0 for s in self._surfaces: if (s == newsurface): return i i = i + 1 self._surfaces.append(newsurface) newsurface.zoom_fit_nice_boundary() self._reset_surface_menu() return len(self._surfaces) - 1 def exit(self): SurfaceManipulator._window_destroyed(self) self._parent.destroy() def find_bundle(self, surface): for surface_bundle in self._surfaces: if surface is surface_bundle.get_surface(): return surface_bundle return None def get_bundle(self): return self._surface def get_bundles(self): return self._surfaces def get_canvas(self): return self._canvas def get_center(self): r""" Return the center of the canvas as a pair of integers. """ return (self.get_width() / 2, self.get_height() / 2) def get_height(self): r""" Return the height of the canvas (an integer). """ return self.get_canvas().winfo_height() def get_parent(self): return self._parent def get_surface_bundle(self): r""" Get the current surface bundle, or None if there is none. """ return self._surface def get_width(self): r""" Return the width of the canvas (an integer). """ return self.get_canvas().winfo_width() def menu_select_surface(self): r""" Called when a surface is selected from a menu. """ i = self._selected_surface.get() if i == -1: self.set_surface(None) else: self.set_surface(self._surfaces[i]) def on_about(self): self.set_text("Written by Vincent Delecroix and Pat Hooper.") def on_delete_junk(self): self._canvas.delete("junk") def on_export(self): r""" Export image as postscript file. """ myFormats = [('PostScript', '*.ps')] fileName = tkFileDialog.asksaveasfilename(parent=self, filetypes=myFormats, title="Save image as...") if len(fileName) > 0: self._canvas.update() self._canvas.postscript(file=fileName) self.set_text("Wrote image to " + fileName) # def on_new_similarity_surface(self): # s = CreateSimilaritySurfaceBundle(len(self._surfaces),self) # if s is not None: # i = self.set_surface(s) # self.set_text("Created new surface `"+self._surfaces[i].get_name()+"'.") def _on_no_surface(self): self._canvas.delete("all") def _on_zoom(self): self.set_actor(ZoomActor(self)) def _on_zoom_box(self): self.set_actor(ZoomBoxActor(self)) def _on_redraw_all(self): if self._surface is not None: self._surface.redraw_all() def _on_recenter(self): self.set_actor(RecenterActor(self)) def _reset_menus(self): r""" Reset all menus except the file and surface menu """ # The following loop removes all but the first two menus (File and Surface). num = len(self._menubar.children) for i in range(num, 2, -1): self._menubar.delete(i) if self._surface != None: self._surface.make_menus(self._menubar) def _reset_surface_menu(self): r""" Reset the surface menu. """ ### This is a hack to get the number of items in the menu: num = self._surface_menu.index(100) + 1 # First we remove everything but the first entry ("None") for i in range(num - 1, 0, -1): #print("removing a child2: "+str(i)+" of "+str(num)) self._surface_menu.delete(i) # Add an entry for every surface in the list. for i in range(len(self._surfaces)): surface = self._surfaces[i] self._surface_menu.add_radiobutton( label=surface.get_name(), command=self.menu_select_surface, variable=self._selected_surface, value=i) def set_text(self, text): self.bottom_text["text"] = text def set_actor(self, actor): r""" Set the current mode of user interaction. """ if (actor != self._currentActor): if self._currentActor != None: self._currentActor.on_deactivate() if (actor == None): self.set_text("Nothing going on.") # Event bindings self._canvas.unbind('<Button-1>') self._canvas.unbind('<Button-2>') self._canvas.unbind('<Button-3>') self._canvas.unbind('<Double-Button-1>') self._canvas.unbind('<Shift-Button-1>') self._canvas.unbind('<Motion>') self.unbind('<FocusIn>') self.unbind('<FocusOut>') #self._canvas.unbind('<ButtonPress-1>') self._canvas.unbind('<ButtonRelease-1>') self._canvas.unbind('<B1-Motion>') self._parent.unbind('<Key>') self._parent.unbind('<KeyRelease>') else: # Event bindings self._canvas.bind('<Button-1>', actor.single_left_click) self._canvas.bind('<Double-Button-1>', actor.double_left_click) self._canvas.bind('<Triple-Button-1>', actor.double_left_click) self._canvas.bind('<Button-2>', actor.single_middle_click) self._canvas.bind('<Double-Button-2>', actor.double_middle_click) self._canvas.bind('<Triple-Button-2>', actor.double_middle_click) self._canvas.bind('<Button-3>', actor.single_right_click) self._canvas.bind('<Double-Button-3>', actor.double_right_click) self._canvas.bind('<Triple-Button-3>', actor.double_right_click) self._canvas.bind('<Shift-Button-1>', actor.shift_click) self._canvas.bind('<Motion>', actor.mouse_moved) #self._canvas.bind('<ButtonPress-1>', actor.left_mouse_pressed) self._canvas.bind('<ButtonRelease-1>', actor.left_mouse_released) self._canvas.bind('<B1-Motion>', actor.left_dragged) self.bind('<FocusIn>', actor.focus_in) self.bind('<FocusOut>', actor.focus_out) self._parent.bind('<Key>', actor.key_press) self._parent.bind('<KeyRelease>', actor.key_release) self._currentActor = actor self._currentActor.on_activate() def set_surface(self, surface_bundle): r""" Set the current surface to the one given by surface_bundle """ i = self.add_surface(surface_bundle) if surface_bundle != self._surface: self._canvas.delete("all") self._surface = surface_bundle self._surface_menu.invoke(i + 1) if i >= 0: self.set_text("Switched to `" + self._surface.get_name() + "'.") self._parent.title(self._surface.get_name()) self._reset_menus() # stop the actor (was a bug). self.set_actor(None) if isinstance(self._surface, EditorRenderer): self._surface.initial_render() else: self.set_text("No surface selected.") self._parent.title("FlatSurf Editor") self._reset_menus() self.set_actor(None) return i def surface_renamed(self): if self._surface is not None: self._parent.title(self._surface.get_name()) self._reset_surface_menu()
class SurfaceManipulator(Frame): r""" A translation surface editor in tk. """ # STATIC METHODS AND OBJECTS current = None # boolean variable to remember if the hook was run! _clear_hook_was_run = 0 @staticmethod def launch(geometry = "800x700+10+10"): r"""Prefered way to gain access to a SurfaceManipulator window.""" if SurfaceManipulator.current is None: SurfaceManipulator._clear_hook() root = Tk() root.geometry(geometry) SurfaceManipulator.current=SurfaceManipulator(root) return SurfaceManipulator.current @staticmethod def _window_destroyed(surface_manipulator): if SurfaceManipulator.current is surface_manipulator: SurfaceManipulator.current = None @staticmethod def _clear_hook(): if not SurfaceManipulator._clear_hook_was_run: # Hack due to Nathan Dunfield (http://trac.sagemath.org/ticket/15152) import IPython.lib.inputhook as ih ih.clear_inputhook() SurfaceManipulator._clear_hook_was_run = 1 # NORMAL METHODS def __init__(self, parent, surface=None, surfaces=[]): r""" INPUT: - ``surfaces`` -- a list of surfaces that the editor may modify - ``surface`` -- surface selected by default - ``parent`` -- parent Tk window """ Frame.__init__(self, parent) self._parent = parent self.pack(fill="both", expand=1) # Run something when closing self._parent.wm_protocol ("WM_DELETE_WINDOW", self.exit) # Surface currently being manipulated self._surface=None # List of surfaces in editor self._surfaces=[] # More variables to initialize self._currentActor=None # Initialization of GUI self._init_menu() self._init_gui() # Setup surface list for s in surfaces: self.add_surface(s) # Setup initial surface if surface is not None: self.add_surface(surface) self.set_surface(surface) def __repr__(self): return "Surface manipulator" def add_mega_wollmilchsau(self): from geometry.mega_wollmilchsau import MegaWollmilchsau s = MegaWollmilchsau() sm,sb = s.get_bundle() self.set_surface(sb) def add_octagon(self): from geometry.similarity_surface_generators import TranslationSurfaceGenerators ss=TranslationSurfaceGenerators.regular_octagon() ss.edit() def _init_menu(self): self._menubar = Menu(self._parent) menubar=self._menubar self._parent.config(menu=menubar) #new_menu = Menu(menubar, tearoff=0) #new_menu.add_command(label="Billiard Table", command=self.on_new_similarity_surface) file_menu = Menu(menubar, tearoff=0) #file_menu.add_cascade(label="New", menu=new_menu) file_menu.add_command(label="Octagon", command=self.add_octagon) file_menu.add_command(label="MegaWollmilchsau", command=self.add_mega_wollmilchsau) file_menu.add_separator() file_menu.add_command(label="About", command=self.on_about) file_menu.add_command(label="Export PostScript", command=self.on_export) file_menu.add_command(label="Exit", command=self.exit, accelerator="Alt+F4") menubar.add_cascade(label="File", underline=0, menu=file_menu) self._surface_menu = Menu(menubar, tearoff=0) self._selected_surface = IntVar() self._selected_surface.set(-1) menubar.add_cascade(label="Surface", underline=0, menu=self._surface_menu) self._surface_menu.add_radiobutton(label="None", command=self.menu_select_surface, variable=self._selected_surface, value=-1) def _init_gui(self): self._parent.title("FlatSurf Editor") self._canvas = Canvas(self, bg="#444", width=300, height=200) self._canvas.pack(fill="both", expand=1) self.bottom_text = Label(self, text="Welcome to FlatSurf.", anchor="w") self.bottom_text.pack(fill="x", expand=0) self.set_actor(None) def add_surface(self,newsurface): r""" Add a surface to the display list for the window. Returns the index of the new surface in the surface list. """ if (newsurface==None): return -1 i=0 for s in self._surfaces: if (s==newsurface): return i i=i+1 self._surfaces.append(newsurface) newsurface.zoom_fit_nice_boundary() self._reset_surface_menu() return len(self._surfaces)-1 def exit(self): SurfaceManipulator._window_destroyed(self) self._parent.destroy() def find_bundle(self, surface): for surface_bundle in self._surfaces: if surface is surface_bundle.get_surface(): return surface_bundle return None def get_bundle(self): return self._surface def get_bundles(self): return self._surfaces def get_canvas(self): return self._canvas def get_center(self): r""" Return the center of the canvas as a pair of integers. """ return ( self.get_width()/2, self.get_height()/2 ) def get_height(self): r""" Return the height of the canvas (an integer). """ return self.get_canvas().winfo_height() def get_parent(self): return self._parent def get_surface_bundle(self): r""" Get the current surface bundle, or None if there is none. """ return self._surface def get_width(self): r""" Return the width of the canvas (an integer). """ return self.get_canvas().winfo_width() def menu_select_surface(self): r""" Called when a surface is selected from a menu. """ i = self._selected_surface.get() if i == -1: self.set_surface(None) else: self.set_surface(self._surfaces[i]) def on_about(self): self.set_text("Written by Vincent Delecroix and Pat Hooper.") def on_delete_junk(self): self._canvas.delete("junk") def on_export(self): r""" Export image as postscript file. """ myFormats = [('PostScript','*.ps')] fileName = tkFileDialog.asksaveasfilename(parent=self, filetypes=myFormats , title="Save image as...") if len(fileName ) > 0: self._canvas.update() self._canvas.postscript(file = fileName) self.set_text("Wrote image to "+fileName) # def on_new_similarity_surface(self): # s = CreateSimilaritySurfaceBundle(len(self._surfaces),self) # if s is not None: # i = self.set_surface(s) # self.set_text("Created new surface `"+self._surfaces[i].get_name()+"'.") def _on_no_surface(self): self._canvas.delete("all") def _on_zoom(self): self.set_actor(ZoomActor(self)) def _on_zoom_box(self): self.set_actor(ZoomBoxActor(self)) def _on_redraw_all(self): if self._surface is not None: self._surface.redraw_all() def _on_recenter(self): self.set_actor(RecenterActor(self)) def _reset_menus(self): r""" Reset all menus except the file and surface menu """ # The following loop removes all but the first two menus (File and Surface). num = len(self._menubar.children) for i in range(num,2,-1): self._menubar.delete(i) if self._surface!= None: self._surface.make_menus(self._menubar) def _reset_surface_menu(self): r""" Reset the surface menu. """ ### This is a hack to get the number of items in the menu: num = self._surface_menu.index(100)+1 # First we remove everything but the first entry ("None") for i in range(num-1,0,-1): #print("removing a child2: "+str(i)+" of "+str(num)) self._surface_menu.delete(i) # Add an entry for every surface in the list. for i in range( len(self._surfaces) ): surface = self._surfaces[i] self._surface_menu.add_radiobutton(label=surface.get_name(), command=self.menu_select_surface, variable=self._selected_surface, value=i) def set_text(self, text): self.bottom_text["text"]=text def set_actor(self, actor): r""" Set the current mode of user interaction. """ if (actor != self._currentActor): if self._currentActor != None: self._currentActor.on_deactivate() if (actor==None): self.set_text("Nothing going on.") # Event bindings self._canvas.unbind('<Button-1>') self._canvas.unbind('<Button-2>') self._canvas.unbind('<Button-3>') self._canvas.unbind('<Double-Button-1>') self._canvas.unbind('<Shift-Button-1>') self._canvas.unbind('<Motion>') self.unbind('<FocusIn>') self.unbind('<FocusOut>') #self._canvas.unbind('<ButtonPress-1>') self._canvas.unbind('<ButtonRelease-1>') self._canvas.unbind('<B1-Motion>') self._parent.unbind('<Key>') self._parent.unbind('<KeyRelease>') else: # Event bindings self._canvas.bind('<Button-1>', actor.single_left_click) self._canvas.bind('<Double-Button-1>', actor.double_left_click) self._canvas.bind('<Triple-Button-1>', actor.double_left_click) self._canvas.bind('<Button-2>', actor.single_middle_click) self._canvas.bind('<Double-Button-2>', actor.double_middle_click) self._canvas.bind('<Triple-Button-2>', actor.double_middle_click) self._canvas.bind('<Button-3>', actor.single_right_click) self._canvas.bind('<Double-Button-3>', actor.double_right_click) self._canvas.bind('<Triple-Button-3>', actor.double_right_click) self._canvas.bind('<Shift-Button-1>', actor.shift_click) self._canvas.bind('<Motion>', actor.mouse_moved) #self._canvas.bind('<ButtonPress-1>', actor.left_mouse_pressed) self._canvas.bind('<ButtonRelease-1>', actor.left_mouse_released) self._canvas.bind('<B1-Motion>',actor.left_dragged) self.bind('<FocusIn>', actor.focus_in) self.bind('<FocusOut>', actor.focus_out) self._parent.bind('<Key>', actor.key_press) self._parent.bind('<KeyRelease>', actor.key_release) self._currentActor=actor self._currentActor.on_activate() def set_surface(self,surface_bundle): r""" Set the current surface to the one given by surface_bundle """ i = self.add_surface(surface_bundle) if surface_bundle != self._surface: self._canvas.delete("all") self._surface=surface_bundle self._surface_menu.invoke(i+1) if i >= 0: self.set_text("Switched to `"+self._surface.get_name()+"'.") self._parent.title(self._surface.get_name()) self._reset_menus() # stop the actor (was a bug). self.set_actor(None) if isinstance(self._surface, EditorRenderer): self._surface.initial_render() else: self.set_text("No surface selected.") self._parent.title("FlatSurf Editor") self._reset_menus() self.set_actor(None) return i def surface_renamed(self): if self._surface is not None: self._parent.title(self._surface.get_name()) self._reset_surface_menu()
class MatrixViewer: def __init__(self,height=300,width=300,filename='tmp.ps'): from Tkinter import Tk, Canvas, Frame, Button self.width=width self.height=height self.root = Tk() self.canvas = Canvas(self.root,width=self.width,height=self.height, background='White') self.canvas.pack() self.toolbar = Frame(self.root) self.dumpbutton = Button(self.toolbar,text="Dump",command=self.dump) self.dumpbutton.pack(side=LEFT) self.quitbutton = Button(self.toolbar,text="Quit", command=self.root.quit) self.quitbutton.pack(side=LEFT) self.toolbar.pack() self.filename = filename return def dump(self): ps = self.canvas.postscript() file = open(self.filename,'w') file.write(ps) file.close() return def spy(self,A,cutoff=0.1): n,m = A.shape if n>self.width or m>self.height: raise "Rectangle too big %d %d %d %d" %\ (n,m,self.width,self.height) for i in range(n): xmin = self.width*i/float(n) xmax = self.width*(i+1)/float(n) for j in range(m): ymin = self.height*j/float(m) ymax = self.height*(j+1)/float(m) if abs(A[i,j]) > cutoff: self.canvas.create_rectangle(xmin,ymin, xmax,ymax,fill='Blue', outline='') else: self.canvas.create_rectangle(xmin,ymin, xmax,ymax,fill='White', outline='') self.root.mainloop() return def pcolor(self,A): n,m = A.shape mina,maxa = get_extrema(A) if n>self.width or m>self.height: raise "Rectangle too big" for i in range(n): xmin = self.width*i/float(n) xmax = self.width*(i+1)/float(n) for j in range(m): ymin = self.height*j/float(m) ymax = self.height*(j+1)/float(m) color = get_color(A[i,j],mina,maxa) self.canvas.create_rectangle(xmin,ymin,xmax,ymax,fill=color) self.root.mainloop() return