def __init__(self): self.root = Tk() self.setUpMenu() self.canvas = self.setUpCanvas(self.root, 800, 800) self.canvas.pack() self.graph = DrawableGraph () self.eventHandler = EventHandler(self.canvas) self.root.mainloop()
def findMin(nodeList): minimum = nodeList[0].getWeight() minNode = nodeList[0] for node in nodeList: if (node.getWeight() < minimum): minNode = node return minNode def findNode(nodeList, vertex): for node in nodeList: if (node.getVertex() == vertex): return node g = DrawableGraph() #a = g.addDrawableVertex((240, 75), 1) #b = g.addDrawableVertex((175, 150), 2) #c = g.addDrawableVertex((305, 150), 3) #d = g.addDrawableVertex((145, 225), 4) #e = g.addDrawableVertex((205, 225), 5) #f = g.addDrawableVertex((275, 225), 6) #h = g.addDrawableVertex((335, 225), 7) #g.addEdge(a, b, 4) #g.addEdge(a, c, 1) #g.addEdge(c, b, 2) #g.addEdge(b, e) #g.addEdge(c, f) #g.addEdge(c, h) #g.addEdge(a, h)
def newGraph(self): self.canvas.delete("all") self.graph = DrawableGraph() self.canvas.unbind("<Button-1>")
class App: def __init__(self): self.root = Tk() self.setUpMenu() self.canvas = self.setUpCanvas(self.root, 800, 800) self.canvas.pack() self.graph = DrawableGraph () self.eventHandler = EventHandler(self.canvas) self.root.mainloop() def setUpMenu(self): menubar = Menu(self.root) menubar.add_command(label="New Graph", command=self.newGraph) menubar.add_command(label="Properties", command=self.properties) menubar.add_command(label="Reset Graph", command = self.resetGraph) algMenu = Menu(menubar, tearoff=0) algMenu.add_command(label="Depth First Search", command=self.dfs) algMenu.add_command(label="Breadth First Search", command=self.bfs) algMenu.add_command(label="Prim's Algorithm", command=self.prims) algMenu.add_command(label="Dijkstra's Algorithm", command=self.dijkstras) menubar.add_cascade(label="Algorithms", menu=algMenu) addMenu = Menu(menubar, tearoff=0) addMenu.add_command(label="Vertices", command=self.addVertices) addMenu.add_command(label="Edges", command=self.addEdges) menubar.add_cascade(label="Add", menu=addMenu) self.root.config(menu=menubar) # Bind mouse click event to add a new vertex def addVertices(self): messagebox.showinfo("Add Vertex", "Click anywhere to add a vertex") self.canvas.bind("<Button-1>", self.newVertex) # Bind mouse click event to add a new edge def addEdges(self): messagebox.showinfo("Add Edge", "Select a start vertex and an end vertex") self.canvas.bind("<Button-1>", self.newEdge) # Wipe canvas, set graph attribute to a new graph def newGraph(self): self.canvas.delete("all") self.graph = DrawableGraph() self.canvas.unbind("<Button-1>") # Colour all edges and vertices black def resetGraph(self): for v in self.graph: v.setColour("black") self.graph.drawGraph(self.canvas) # Tell eventHandler to add a new edge def newEdge(self, event): self.eventHandler.newEdge(event, self.graph) # Get properties back from eventHandler and display def properties(self): properties = self.eventHandler.handleProperties(self.graph) # Graph has no edges or vertices, so do not show anything if (properties != None): PropertiesDialog(self.root, properties) # Bind mouse click event to DFS def dfs(self): messagebox.showinfo("DFS", "Click on a start vertex") self.canvas.bind("<Button-1>", self.handleDFS) def handleDFS(self, event): self.eventHandler.handleDFS(self.graph, event) # Bind mouse click event to BFS def bfs(self): messagebox.showinfo("BFS", "Click on a start vertex") self.canvas.bind("<Button-1>", self.handleBFS) def handleBFS(self, event): self.eventHandler.handleBFS(self.graph, event) # Bind mouse click event to Prims def prims(self): messagebox.showinfo("Prims", "Click on a start vertex") self.canvas.bind("<Button-1>", self.handlePrims) def handlePrims(self, event): self.eventHandler.handlePrims(self.graph, event) # Bind mouse click event to Dijkstras def dijkstras(self): messagebox.showinfo("Dijkstras", "Click on a start vertex") self.canvas.bind("<Button-1>", self.handleDijkstra) def handleDijkstra(self, event): self.eventHandler.handleDijkstra(self.graph, event) def draw(self): self.graph.drawGraph(self.canvas) def setUpCanvas(self, master, width, height): canvas = Canvas(master, width=width, height=height) return canvas def newVertex(self, event): self.eventHandler.newVertex(event, self.graph)