def initUI(self): self.parent.title("ANCR-GUI") # Create left and right frames and packs them within the parent frame self.leftFrame = Frame(self.parent, bg='light blue', height=700, width=700) #light colored bg to see panel self.rightFrame = Frame(self.parent, bg="dark gray", height=700, width=400) #dark colored bg to see panel self.leftFrame.pack(side="left", fill="both", expand=1) #self.leftFrame.pack_propagate(0) self.rightFrame.pack(side="right", fill="y", expand=0) #self.rightFrame.pack_propagate(0) # Creates a scrollbar on the right frame and corresponding window which it controls self.rightSideCanvas = Canvas(self.rightFrame, height=700, width=400, bg='dark gray', highlightbackground='dark gray', highlightthickness=0) self.rightCanvasFrame = Frame(self.rightSideCanvas, bg='dark gray') self.vsb = Scrollbar(self.rightFrame, orient="vertical", command=self.rightSideCanvas.yview) self.rightSideCanvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.rightSideCanvas.pack(side="left", expand=True) self.rightSideCanvas.create_window((0,0), window=self.rightCanvasFrame, anchor="nw") self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) # handles creation of canvas and miniframes self.rightCanvasFrame.bind("<Configure>", self.onFrameConfigure) # enables scrollbar functionality self.createTabs()
def initUI(self): self.parent.title("ANCR-GUI") # Create left and right frames and packs them within the parent frame self.leftFrame = Frame(self.parent, bg='light blue', height=700, width=700) #light colored bg to see panel self.rightFrame = Frame(self.parent, bg="dark gray", height=700, width=400) #dark colored bg to see panel self.leftFrame.pack(side="left", fill="both", expand=1) #self.leftFrame.pack_propagate(0) self.rightFrame.pack(side="right", fill="y", expand=0) #self.rightFrame.pack_propagate(0) # Creates a scrollbar on the right frame and corresponding window which it controls self.rightSideCanvas = Canvas(self.rightFrame, height=700, width=400, bg='dark gray', highlightbackground='dark gray', highlightthickness=0) self.rightCanvasFrame = Frame(self.rightSideCanvas, bg='dark gray') self.vsb = Scrollbar(self.rightFrame, orient="vertical", command=self.rightSideCanvas.yview) self.rightSideCanvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.rightSideCanvas.pack(side="left", expand=True) self.rightSideCanvas.create_window((0, 0), window=self.rightCanvasFrame, anchor="nw") self.geoCanvas = CanvasFrame( self.leftFrame, self.rightCanvasFrame, self.G, self.D) # handles creation of canvas and miniframes self.rightCanvasFrame.bind( "<Configure>", self.onFrameConfigure) # enables scrollbar functionality self.createTabs()
def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) # init main frame self.container = tk.Frame(self) self.container.pack(side="top", fill="both", expand = True) # self.container.grid(row = 0, column = 0, sticky = "nswe") self.frames = {} # init frame add to dict frame = CanvasFrame.CanvasFrame(self.container, self) self.frames[CanvasFrame] = frame frame.grid(row = 0, column = 0, sticky = "nswe") # init frame add to dict frame = StartFrame.StartFrame(self.container, self) self.frames[StartFrame] = frame frame.grid(row = 0, column = 0, sticky = "nswe") # add close window event handler self.protocol("WM_DELETE_WINDOW", self.onClossing) # load canvas self.show_frame(CanvasFrame)
def open(self, event=None): fileExt = tkFileDialog.askopenfilename() if fileExt != '': self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename)-1]) self.G = pickle.load(open(str(self.filename))) self.G = nx.convert_node_labels_to_integers(self.G, first_label=1) for widget in self.leftFrame.winfo_children(): widget.destroy() self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) self.createTabs() # redraw nodes for nodeNum in self.G.nodes(): r = 8 if 'Type' in self.G.node[nodeNum] and self.G.node[nodeNum]['Type'] == 'Compartment': self.geoCanvas.systemsCanvas.create_oval(self.G.node[nodeNum]['x_coord']-r, self.G.node[nodeNum]['y_coord']-r, self.G.node[nodeNum]['x_coord']+r, self.G.node[nodeNum]['y_coord']+r, fill='blue', tag='node') else: self.geoCanvas.systemsCanvas.create_oval(self.G.node[nodeNum]['x_coord']-r, self.G.node[nodeNum]['y_coord']-r, self.G.node[nodeNum]['x_coord']+r, self.G.node[nodeNum]['y_coord']+r, fill='red', tag='node') # redraw edges for startNode, endNode in self.G.edges(): edgeItem=self.geoCanvas.systemsCanvas.create_line(self.G.edge[startNode][endNode]['x1_coord'], self.G.edge[startNode][endNode]['y1_coord'], self.G.edge[startNode][endNode]['x2_coord'], self.G.edge[startNode][endNode]['y2_coord'], tag='edge') self.geoCanvas.systemsCanvas.addtag_withtag(str(startNode), edgeItem) self.geoCanvas.systemsCanvas.addtag_withtag(str(endNode), edgeItem) self.G.edge[startNode][endNode]['edgeID'] = edgeItem # reload demands self.geoCanvas.manager.systems = self.G.node[1]['systems'] for key in self.geoCanvas.manager.systems: self.geoCanvas.optionList.insert(len(self.geoCanvas.optionList)-2, key) self.geoCanvas.dropdown.destroy() self.geoCanvas.dropdown = OptionMenu(self.geoCanvas.toolbar, self.geoCanvas.v, *self.geoCanvas.optionList, command=self.geoCanvas.newOptionMenu) self.geoCanvas.dropdown.configure(bg="light blue") self.geoCanvas.dropdown.pack(side='left') # add to log file log = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ": Opened file: " + str(self.filename) self.geoCanvas.appendLog(log)
class Window(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.G = nx.DiGraph() self.D = nx.DiGraph() self.initUI() # exits out of gui when clicked on def exit(self, event=None): self.quit() # will update saved file if filename already exists or open up save as function def save(self, event=None): if hasattr(self, 'filename'): pickle.dump(self.G, open(str(self.filename), 'w')) # add to log file s = str(self.filename) filename = s.split('/') log = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ": Saved file: " + filename[len(filename)-1] self.geoCanvas.appendLog(log) else: self.save_as() # can save network x graph (node/edges and attributes) as any type of text file def save_as(self, event=None): for node in self.G.nodes(): self.G.node[node]['systems'] = self.geoCanvas.manager.systems fileExt = tkFileDialog.asksaveasfilename(defaultextension=".txt") if fileExt != '': # asksaveasfile returns '' if dialog closed with "cancel". self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename)-1]) pickle.dump(self.G, open(str(self.filename), 'w')) # add to log file log = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ": Saved file as: " + str(self.filename) self.geoCanvas.appendLog(log) # can open any previously saved network x graph and plot nodes and edges onto the canvas, and resume all gui functionality def open(self, event=None): fileExt = tkFileDialog.askopenfilename() if fileExt != '': self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename)-1]) self.G = pickle.load(open(str(self.filename))) self.G = nx.convert_node_labels_to_integers(self.G, first_label=1) for widget in self.leftFrame.winfo_children(): widget.destroy() self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) self.createTabs() # redraw nodes for nodeNum in self.G.nodes(): r = 8 if 'Type' in self.G.node[nodeNum] and self.G.node[nodeNum]['Type'] == 'Compartment': self.geoCanvas.systemsCanvas.create_oval(self.G.node[nodeNum]['x_coord']-r, self.G.node[nodeNum]['y_coord']-r, self.G.node[nodeNum]['x_coord']+r, self.G.node[nodeNum]['y_coord']+r, fill='blue', tag='node') else: self.geoCanvas.systemsCanvas.create_oval(self.G.node[nodeNum]['x_coord']-r, self.G.node[nodeNum]['y_coord']-r, self.G.node[nodeNum]['x_coord']+r, self.G.node[nodeNum]['y_coord']+r, fill='red', tag='node') # redraw edges for startNode, endNode in self.G.edges(): edgeItem=self.geoCanvas.systemsCanvas.create_line(self.G.edge[startNode][endNode]['x1_coord'], self.G.edge[startNode][endNode]['y1_coord'], self.G.edge[startNode][endNode]['x2_coord'], self.G.edge[startNode][endNode]['y2_coord'], tag='edge') self.geoCanvas.systemsCanvas.addtag_withtag(str(startNode), edgeItem) self.geoCanvas.systemsCanvas.addtag_withtag(str(endNode), edgeItem) self.G.edge[startNode][endNode]['edgeID'] = edgeItem # reload demands self.geoCanvas.manager.systems = self.G.node[1]['systems'] for key in self.geoCanvas.manager.systems: self.geoCanvas.optionList.insert(len(self.geoCanvas.optionList)-2, key) self.geoCanvas.dropdown.destroy() self.geoCanvas.dropdown = OptionMenu(self.geoCanvas.toolbar, self.geoCanvas.v, *self.geoCanvas.optionList, command=self.geoCanvas.newOptionMenu) self.geoCanvas.dropdown.configure(bg="light blue") self.geoCanvas.dropdown.pack(side='left') # add to log file log = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ": Opened file: " + str(self.filename) self.geoCanvas.appendLog(log) # creates the gui menubar def createTabs(self): # Binds submenus to their shortcut key; use ctrl for windows and cmd for OS X if _platform == "win32": self.parent.bind('<Control-z>', self.geoCanvas.undo) self.parent.bind('<Control-Z>', self.geoCanvas.redo) self.parent.bind('<Control-s>', self.save) self.parent.bind('<Control-S>', self.save_as) self.parent.bind('<Control-o>', self.open) self.parent.bind('<Control-q>', self.exit) accelPrefix = "Ctrl-" else: self.parent.bind('<Command-z>', self.geoCanvas.undo) self.parent.bind('<Command-Z>', self.geoCanvas.redo) self.parent.bind('<Command-s>', self.save) self.parent.bind('<Command-S>', self.save_as) self.parent.bind('<Command-o>', self.open) self.parent.bind('<Command-q>', self.exit) accelPrefix = "Cmd-" # MAIN MENUBAR menubar = Menu(self.parent) self.parent.config(menu=menubar) # File Tab fileTab = Menu(menubar, tearoff=0) fileTab.add_command(label="Open...", command=self.open, accelerator=accelPrefix+"O") fileTab.add_command(label="Save", command=self.save, accelerator=accelPrefix+"S") fileTab.add_command(label="Save As...", command=self.save_as, accelerator=accelPrefix+"Shift-S") fileTab.add_command(label="Exit", command=self.exit, accelerator=accelPrefix+"Q") menubar.add_cascade(label="File", menu=fileTab) # Edit Tab editTab = Menu(menubar, tearoff=0) editTab.add_command(label="Undo", command=self.geoCanvas.undo, accelerator=accelPrefix+"Z") editTab.add_command(label="Redo", command=self.geoCanvas.redo, accelerator=accelPrefix+"Shift-Z") menubar.add_cascade(label="Edit", menu=editTab) #View Tab viewTab = Menu(menubar, tearoff=0) viewTab.add_command(label="Show Labels", command=self.geoCanvas.showLabels) viewTab.add_command(label="Hide Labels", command=self.geoCanvas.hideLabels) menubar.add_cascade(label="View", menu=viewTab) #Window Tab windowTab = Menu(menubar, tearoff=0) windowTab.add_command(label='Log Window', command=self.geoCanvas.dockedWindows.logWindow) windowTab.add_command(label='Component Geometry', command=lambda:viewComponentGeo(self.G)) windowTab.add_command(label='Compartment Geometry', command=lambda:viewCompartmentGeo(self.G)) menubar.add_cascade(label="Windows", menu=windowTab) #Analysis Tab analysisTab = Menu(menubar, tearoff=0) analysisTab.add_command(label="Node Degrees", command=self.geoCanvas.dockedWindows.nodeDegrees) analysisTab.add_command(label='Centrality', command=self.geoCanvas.dockedWindows.centrality) menubar.add_cascade(label="Analysis", menu=analysisTab) def initUI(self): self.parent.title("ANCR-GUI") # Create left and right frames and packs them within the parent frame self.leftFrame = Frame(self.parent, bg='light blue', height=700, width=700) #light colored bg to see panel self.rightFrame = Frame(self.parent, bg="dark gray", height=700, width=400) #dark colored bg to see panel self.leftFrame.pack(side="left", fill="both", expand=1) #self.leftFrame.pack_propagate(0) self.rightFrame.pack(side="right", fill="y", expand=0) #self.rightFrame.pack_propagate(0) # Creates a scrollbar on the right frame and corresponding window which it controls self.rightSideCanvas = Canvas(self.rightFrame, height=700, width=400, bg='dark gray', highlightbackground='dark gray', highlightthickness=0) self.rightCanvasFrame = Frame(self.rightSideCanvas, bg='dark gray') self.vsb = Scrollbar(self.rightFrame, orient="vertical", command=self.rightSideCanvas.yview) self.rightSideCanvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.rightSideCanvas.pack(side="left", expand=True) self.rightSideCanvas.create_window((0,0), window=self.rightCanvasFrame, anchor="nw") self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) # handles creation of canvas and miniframes self.rightCanvasFrame.bind("<Configure>", self.onFrameConfigure) # enables scrollbar functionality self.createTabs() # set the right frame window to match the scroll bar configure def onFrameConfigure(self, event): self.rightSideCanvas.configure(scrollregion=self.rightSideCanvas.bbox("all"), width=400, height=700)
def open(self, event=None): fileExt = tkFileDialog.askopenfilename() if fileExt != '': self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename) - 1]) self.G = pickle.load(open(str(self.filename))) self.G = nx.convert_node_labels_to_integers(self.G, first_label=1) for widget in self.leftFrame.winfo_children(): widget.destroy() self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) self.createTabs() # redraw nodes for nodeNum in self.G.nodes(): r = 8 if 'Type' in self.G.node[nodeNum] and self.G.node[nodeNum][ 'Type'] == 'Compartment': self.geoCanvas.systemsCanvas.create_oval( self.G.node[nodeNum]['x_coord'] - r, self.G.node[nodeNum]['y_coord'] - r, self.G.node[nodeNum]['x_coord'] + r, self.G.node[nodeNum]['y_coord'] + r, fill='blue', tag='node') else: self.geoCanvas.systemsCanvas.create_oval( self.G.node[nodeNum]['x_coord'] - r, self.G.node[nodeNum]['y_coord'] - r, self.G.node[nodeNum]['x_coord'] + r, self.G.node[nodeNum]['y_coord'] + r, fill='red', tag='node') # redraw edges for startNode, endNode in self.G.edges(): edgeItem = self.geoCanvas.systemsCanvas.create_line( self.G.edge[startNode][endNode]['x1_coord'], self.G.edge[startNode][endNode]['y1_coord'], self.G.edge[startNode][endNode]['x2_coord'], self.G.edge[startNode][endNode]['y2_coord'], tag='edge') self.geoCanvas.systemsCanvas.addtag_withtag( str(startNode), edgeItem) self.geoCanvas.systemsCanvas.addtag_withtag( str(endNode), edgeItem) self.G.edge[startNode][endNode]['edgeID'] = edgeItem # reload demands self.geoCanvas.manager.systems = self.G.node[1]['systems'] for key in self.geoCanvas.manager.systems: self.geoCanvas.optionList.insert( len(self.geoCanvas.optionList) - 2, key) self.geoCanvas.dropdown.destroy() self.geoCanvas.dropdown = OptionMenu( self.geoCanvas.toolbar, self.geoCanvas.v, *self.geoCanvas.optionList, command=self.geoCanvas.newOptionMenu) self.geoCanvas.dropdown.configure(bg="light blue") self.geoCanvas.dropdown.pack(side='left') # add to log file log = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + ": Opened file: " + str(self.filename) self.geoCanvas.appendLog(log)
class Window(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.G = nx.DiGraph() self.D = nx.DiGraph() self.initUI() # exits out of gui when clicked on def exit(self, event=None): self.quit() # will update saved file if filename already exists or open up save as function def save(self, event=None): if hasattr(self, 'filename'): pickle.dump(self.G, open(str(self.filename), 'w')) # add to log file s = str(self.filename) filename = s.split('/') log = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + ": Saved file: " + filename[ len(filename) - 1] self.geoCanvas.appendLog(log) else: self.save_as() # can save network x graph (node/edges and attributes) as any type of text file def save_as(self, event=None): for node in self.G.nodes(): self.G.node[node]['systems'] = self.geoCanvas.manager.systems fileExt = tkFileDialog.asksaveasfilename(defaultextension=".txt") if fileExt != '': # asksaveasfile returns '' if dialog closed with "cancel". self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename) - 1]) pickle.dump(self.G, open(str(self.filename), 'w')) # add to log file log = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + ": Saved file as: " + str(self.filename) self.geoCanvas.appendLog(log) # can open any previously saved network x graph and plot nodes and edges onto the canvas, and resume all gui functionality def open(self, event=None): fileExt = tkFileDialog.askopenfilename() if fileExt != '': self.filename = fileExt s = str(self.filename) filename = s.split('/') self.parent.title("ANCR-GUI - " + filename[len(filename) - 1]) self.G = pickle.load(open(str(self.filename))) self.G = nx.convert_node_labels_to_integers(self.G, first_label=1) for widget in self.leftFrame.winfo_children(): widget.destroy() self.geoCanvas = CanvasFrame(self.leftFrame, self.rightCanvasFrame, self.G, self.D) self.createTabs() # redraw nodes for nodeNum in self.G.nodes(): r = 8 if 'Type' in self.G.node[nodeNum] and self.G.node[nodeNum][ 'Type'] == 'Compartment': self.geoCanvas.systemsCanvas.create_oval( self.G.node[nodeNum]['x_coord'] - r, self.G.node[nodeNum]['y_coord'] - r, self.G.node[nodeNum]['x_coord'] + r, self.G.node[nodeNum]['y_coord'] + r, fill='blue', tag='node') else: self.geoCanvas.systemsCanvas.create_oval( self.G.node[nodeNum]['x_coord'] - r, self.G.node[nodeNum]['y_coord'] - r, self.G.node[nodeNum]['x_coord'] + r, self.G.node[nodeNum]['y_coord'] + r, fill='red', tag='node') # redraw edges for startNode, endNode in self.G.edges(): edgeItem = self.geoCanvas.systemsCanvas.create_line( self.G.edge[startNode][endNode]['x1_coord'], self.G.edge[startNode][endNode]['y1_coord'], self.G.edge[startNode][endNode]['x2_coord'], self.G.edge[startNode][endNode]['y2_coord'], tag='edge') self.geoCanvas.systemsCanvas.addtag_withtag( str(startNode), edgeItem) self.geoCanvas.systemsCanvas.addtag_withtag( str(endNode), edgeItem) self.G.edge[startNode][endNode]['edgeID'] = edgeItem # reload demands self.geoCanvas.manager.systems = self.G.node[1]['systems'] for key in self.geoCanvas.manager.systems: self.geoCanvas.optionList.insert( len(self.geoCanvas.optionList) - 2, key) self.geoCanvas.dropdown.destroy() self.geoCanvas.dropdown = OptionMenu( self.geoCanvas.toolbar, self.geoCanvas.v, *self.geoCanvas.optionList, command=self.geoCanvas.newOptionMenu) self.geoCanvas.dropdown.configure(bg="light blue") self.geoCanvas.dropdown.pack(side='left') # add to log file log = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') + ": Opened file: " + str(self.filename) self.geoCanvas.appendLog(log) # creates the gui menubar def createTabs(self): # Binds submenus to their shortcut key; use ctrl for windows and cmd for OS X if _platform == "win32": self.parent.bind('<Control-z>', self.geoCanvas.undo) self.parent.bind('<Control-Z>', self.geoCanvas.redo) self.parent.bind('<Control-s>', self.save) self.parent.bind('<Control-S>', self.save_as) self.parent.bind('<Control-o>', self.open) self.parent.bind('<Control-q>', self.exit) accelPrefix = "Ctrl-" else: self.parent.bind('<Command-z>', self.geoCanvas.undo) self.parent.bind('<Command-Z>', self.geoCanvas.redo) self.parent.bind('<Command-s>', self.save) self.parent.bind('<Command-S>', self.save_as) self.parent.bind('<Command-o>', self.open) self.parent.bind('<Command-q>', self.exit) accelPrefix = "Cmd-" # MAIN MENUBAR menubar = Menu(self.parent) self.parent.config(menu=menubar) # File Tab fileTab = Menu(menubar, tearoff=0) fileTab.add_command(label="Open...", command=self.open, accelerator=accelPrefix + "O") fileTab.add_command(label="Save", command=self.save, accelerator=accelPrefix + "S") fileTab.add_command(label="Save As...", command=self.save_as, accelerator=accelPrefix + "Shift-S") fileTab.add_command(label="Exit", command=self.exit, accelerator=accelPrefix + "Q") menubar.add_cascade(label="File", menu=fileTab) # Edit Tab editTab = Menu(menubar, tearoff=0) editTab.add_command(label="Undo", command=self.geoCanvas.undo, accelerator=accelPrefix + "Z") editTab.add_command(label="Redo", command=self.geoCanvas.redo, accelerator=accelPrefix + "Shift-Z") menubar.add_cascade(label="Edit", menu=editTab) #View Tab viewTab = Menu(menubar, tearoff=0) viewTab.add_command(label="Show Labels", command=self.geoCanvas.showLabels) viewTab.add_command(label="Hide Labels", command=self.geoCanvas.hideLabels) menubar.add_cascade(label="View", menu=viewTab) #Window Tab windowTab = Menu(menubar, tearoff=0) windowTab.add_command(label='Log Window', command=self.geoCanvas.dockedWindows.logWindow) windowTab.add_command(label='Component Geometry', command=lambda: viewComponentGeo(self.G)) windowTab.add_command(label='Compartment Geometry', command=lambda: viewCompartmentGeo(self.G)) menubar.add_cascade(label="Windows", menu=windowTab) #Analysis Tab analysisTab = Menu(menubar, tearoff=0) analysisTab.add_command( label="Node Degrees", command=self.geoCanvas.dockedWindows.nodeDegrees) analysisTab.add_command( label='Centrality', command=self.geoCanvas.dockedWindows.centrality) menubar.add_cascade(label="Analysis", menu=analysisTab) def initUI(self): self.parent.title("ANCR-GUI") # Create left and right frames and packs them within the parent frame self.leftFrame = Frame(self.parent, bg='light blue', height=700, width=700) #light colored bg to see panel self.rightFrame = Frame(self.parent, bg="dark gray", height=700, width=400) #dark colored bg to see panel self.leftFrame.pack(side="left", fill="both", expand=1) #self.leftFrame.pack_propagate(0) self.rightFrame.pack(side="right", fill="y", expand=0) #self.rightFrame.pack_propagate(0) # Creates a scrollbar on the right frame and corresponding window which it controls self.rightSideCanvas = Canvas(self.rightFrame, height=700, width=400, bg='dark gray', highlightbackground='dark gray', highlightthickness=0) self.rightCanvasFrame = Frame(self.rightSideCanvas, bg='dark gray') self.vsb = Scrollbar(self.rightFrame, orient="vertical", command=self.rightSideCanvas.yview) self.rightSideCanvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.rightSideCanvas.pack(side="left", expand=True) self.rightSideCanvas.create_window((0, 0), window=self.rightCanvasFrame, anchor="nw") self.geoCanvas = CanvasFrame( self.leftFrame, self.rightCanvasFrame, self.G, self.D) # handles creation of canvas and miniframes self.rightCanvasFrame.bind( "<Configure>", self.onFrameConfigure) # enables scrollbar functionality self.createTabs() # set the right frame window to match the scroll bar configure def onFrameConfigure(self, event): self.rightSideCanvas.configure( scrollregion=self.rightSideCanvas.bbox("all"), width=400, height=700)
def _init_workspace(self, parent): self._workspace = CanvasFrame(parent, background='white') self._workspace.pack(side='right', fill='both', expand=1) self._tree = None self.reset_workspace()
class CFGDemo(object): def __init__(self, grammar, text): self._grammar = grammar self._text = text # Set up the main window. self._top = Tk() self._top.title('Context Free Grammar Demo') # Base font size self._size = IntVar(self._top) self._size.set(12) # = medium # Set up the key bindings self._init_bindings(self._top) # Create the basic frames frame1 = Frame(self._top) frame1.pack(side='left', fill='y', expand=0) self._init_menubar(self._top) self._init_buttons(self._top) self._init_grammar(frame1) self._init_treelet(frame1) self._init_workspace(self._top) #////////////////////////////////////////////////// # Initialization #////////////////////////////////////////////////// def _init_bindings(self, top): top.bind('<Control-q>', self.destroy) def _init_menubar(self, parent): pass def _init_buttons(self, parent): pass def _init_grammar(self, parent): self._prodlist = ProductionList(parent, self._grammar, width=20) self._prodlist.pack(side='top', fill='both', expand=1) self._prodlist.focus() self._prodlist.add_callback('select', self._selectprod_cb) self._prodlist.add_callback('move', self._selectprod_cb) def _init_treelet(self, parent): self._treelet_canvas = Canvas(parent, background='white') self._treelet_canvas.pack(side='bottom', fill='x') self._treelet = None def _init_workspace(self, parent): self._workspace = CanvasFrame(parent, background='white') self._workspace.pack(side='right', fill='both', expand=1) self._tree = None self.reset_workspace() #////////////////////////////////////////////////// # Workspace #////////////////////////////////////////////////// def reset_workspace(self): c = self._workspace.canvas() fontsize = int(self._size.get()) node_font = ('helvetica', -(fontsize+4), 'bold') leaf_font = ('helvetica', -(fontsize+2)) # Remove the old tree if self._tree is not None: self._workspace.remove_widget(self._tree) # The root of the tree. start = self._grammar.start().symbol() rootnode = TextWidget(c, start, font=node_font, draggable=1) # The leaves of the tree. leaves = [] for word in self._text: leaves.append(TextWidget(c, word, font=leaf_font, draggable=1)) # Put it all together into one tree self._tree = TreeSegmentWidget(c, rootnode, leaves, color='white') # Add it to the workspace. self._workspace.add_widget(self._tree) # Move the leaves to the bottom of the workspace. for leaf in leaves: leaf.move(0,100) #self._nodes = {start:1} #self._leaves = dict([(l,1) for l in leaves]) def workspace_markprod(self, production): pass def _markproduction(self, prod, tree=None): if tree is None: tree = self._tree for i in range(len(tree.subtrees())-len(prod.rhs())): if tree['color', i] == 'white': self._markproduction for j, node in enumerate(prod.rhs()): widget = tree.subtrees()[i+j] if (isinstance(node, Nonterminal) and isinstance(widget, TreeSegmentWidget) and node.symbol == widget.node().text()): pass # matching nonterminal elif (isinstance(node, (str, unicode)) and isinstance(widget, TextWidget) and node == widget.text()): pass # matching nonterminal else: break else: # Everything matched! print 'MATCH AT', i #////////////////////////////////////////////////// # Grammar #////////////////////////////////////////////////// def _selectprod_cb(self, production): canvas = self._treelet_canvas self._prodlist.highlight(production) if self._treelet is not None: self._treelet.destroy() # Convert the production to a tree. rhs = production.rhs() for (i, elt) in enumerate(rhs): if isinstance(elt, Nonterminal): elt = Tree(elt) tree = Tree(production.lhs().symbol(), *rhs) # Draw the tree in the treelet area. fontsize = int(self._size.get()) node_font = ('helvetica', -(fontsize+4), 'bold') leaf_font = ('helvetica', -(fontsize+2)) self._treelet = tree_to_treesegment(canvas, tree, node_font=node_font, leaf_font=leaf_font) self._treelet['draggable'] = 1 # Center the treelet. (x1, y1, x2, y2) = self._treelet.bbox() w, h = int(canvas['width']), int(canvas['height']) self._treelet.move((w-x1-x2)/2, (h-y1-y2)/2) # Mark the places where we can add it to the workspace. self._markproduction(production) def destroy(self, *args): self._top.destroy() def mainloop(self, *args, **kwargs): self._top.mainloop(*args, **kwargs)