# -*- encoding: utf-8 -*- from head import * from NODE import * N = Node() class Edge: def __init__(self): self.mark = [] # 记录已标记的节点数目(连接操作) self.user_cur = 0 # 记录用户所在点 def node_edge(self, no, n1, n2): # 根据节点返回编号 if n1 > n2: n = n1 n1 = n2 n2 = n for e in d.Edges: if e[1] == n1 and e[2] == n2 and e[8] == no: return e[0] return -1 def edge_mark_display(self): for w in frame_right.winfo_children(): w.destroy() # 清除右侧组件 cura = "无" cur1 = -1 curb = "无" cur2 = -1 lab = tk.Label(frame_right, text="连接操作", bg=RED, fg=BLACK, font=('微软雅黑', 20)) lab.place(relx=0.18, rely=0.02, width=200, height=30) for i in d.Nodes: if i[2] == 1: cura = str(i[1]) cur1 = i[0] if i[2] == 2: curb = str(i[1]) cur2 = i[0] E = 0 for e in d.Edges: if ((cur1 == e[1] and cur2 == e[2]) or (cur2 == e[1] and cur1 == e[2])): if e[8] == 1 and e[3] == 1 and E < 1: E = 1 elif e[8] == 2 and e[3] == 1 and E < 2: E = 2 elif e[8] == 3 and e[3] == 1 and E < 3: E = 3 y0 = 0.1 B = tk.Button(frame_right, text="建立为完全图", command=lambda: self.edge_addall()) B.place(relx=0.15, rely=y0 - 0.02, width=80, height=30) B = tk.Button(frame_right, text="删除所有链接", command=lambda: self.edge_delall()) B.place(relx=0.65, rely=y0 - 0.02, width=80, height=30) # 当前两个节点信息 laba1 = tk.Label(frame_right, text="当前节点1:", bg=RED, fg=BLACK, font=('微软雅黑', 12)) laba2 = tk.Label(frame_right, text=f"{cura}", bg=BLUE, fg=BLACK, font=('微软雅黑', 8)) laba1.place(relx=0.05, rely=0.02 + y0, width=80, height=30) laba2.place(relx=0.35, rely=0.02 + y0, width=50, height=30) labb1 = tk.Label(frame_right, text="当前节点2:", bg=RED, fg=BLACK, font=('微软雅黑', 12)) labb2 = tk.Label(frame_right, text=f"{curb}", bg=BLUE, fg=BLACK, font=('微软雅黑', 8)) labb1.place(relx=0.52, rely=0.02 + y0, width=80, height=30) labb2.place(relx=0.82, rely=0.02 + y0, width=50, height=30) # 当前链接信息 cur = -1 for edge in d.Edges: if edge[8] == 1 and edge[3] == 1 and d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: cur = edge[0] if E == 0: text = "两点间不存在链接" else: text = "两点间存在" + str(E) + "条链接" lab1 = tk.Label(frame_right, text=f"{text}", bg=RED, fg=BLACK, font=('微软雅黑', 15)) lab1.place(relx=0.05, rely=0.08 + y0, width=200, height=30) if E == 0: B = tk.Button(frame_right, text="建立链接", command=lambda: self.edge_add()) B.place(relx=0.7, rely=0.08 + y0, width=80, height=30) elif E == 1: B = tk.Button(frame_right, text="删除链接", command=lambda: self.edge_del()) B.place(relx=0.7, rely=0.08 + y0, width=80, height=30) if not E == 0: y0 += 0.1 lab = tk.Label(frame_right, text="链接一", bg=RED, fg=BLACK, font=('微软雅黑', 15)) lab.place(relx=0.1, rely=0.08 + y0, width=150, height=30) lab1 = tk.Label(frame_right, text="当前链接", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][5]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 8)) B = tk.Button(frame_right, text="设置标签", command=lambda: self.edge_rename()) lab1.place(relx=0.1, rely=0.13 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.13 + y0, width=80, height=30) B.place(relx=0.7, rely=0.13 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接成本", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][6]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置成本", command=lambda: self.edge_recost()) lab1.place(relx=0.1, rely=0.18 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.18 + y0, width=80, height=30) B.place(relx=0.7, rely=0.18 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接时间", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][7]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置时间", command=lambda: self.edge_retime()) lab1.place(relx=0.1, rely=0.23 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.23 + y0, width=80, height=30) B.place(relx=0.7, rely=0.23 + y0, width=80, height=30) # print ("cur",cur1,cur2) # print (E) if E == 1: B = tk.Button(frame_right, text="增加一条连接", command=lambda: self.newedge_add(2, cur1, cur2)) B.place(relx=0.1, rely=0.3 + y0, width=150, height=30) elif E >= 2: cur = self.node_edge(2, cur1, cur2) lab = tk.Label(frame_right, text="链接二", bg=RED, fg=BLACK, font=('微软雅黑', 15)) lab.place(relx=0.1, rely=0.28 + y0, width=150, height=30) lab1 = tk.Label(frame_right, text="当前链接", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][5]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 8)) B = tk.Button(frame_right, text="设置标签", command=lambda n=cur: self.newedge_rename(n)) lab1.place(relx=0.1, rely=0.33 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.33 + y0, width=80, height=30) B.place(relx=0.7, rely=0.33 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接成本", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][6]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置成本", command=lambda n=cur: self.newedge_recost(n)) lab1.place(relx=0.1, rely=0.38 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.38 + y0, width=80, height=30) B.place(relx=0.7, rely=0.38 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接时间", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][7]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置时间", command=lambda n=cur: self.newedge_retime(n)) lab1.place(relx=0.1, rely=0.43 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.43 + y0, width=80, height=30) B.place(relx=0.7, rely=0.43 + y0, width=80, height=30) if E == 2: B = tk.Button(frame_right, text="增加一条连接", command=lambda: self.newedge_add(3, cur1, cur2)) B.place(relx=0.1, rely=0.5 + y0, width=150, height=30) B = tk.Button(frame_right, text="删除一条连接", command=lambda: self.newedge_del(2, cur1, cur2)) B.place(relx=0.1, rely=0.55 + y0, width=150, height=30) B = tk.Button(frame_right, text="删除多余连接", command=lambda: self.newedge_del_all(1)) B.place(relx=0.1, rely=0.6 + y0, width=150, height=30) elif E == 3: cur = self.node_edge(3, cur1, cur2) y0 += 0.2 lab = tk.Label(frame_right, text="链接三", bg=RED, fg=BLACK, font=('微软雅黑', 15)) lab.place(relx=0.1, rely=0.28 + y0, width=150, height=30) lab1 = tk.Label(frame_right, text="当前链接", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][5]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 8)) B = tk.Button(frame_right, text="设置标签", command=lambda n=cur: self.newedge_rename(n)) lab1.place(relx=0.1, rely=0.33 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.33 + y0, width=80, height=30) B.place(relx=0.7, rely=0.33 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接成本", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][6]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置成本", command=lambda n=cur: self.newedge_recost(n)) lab1.place(relx=0.1, rely=0.38 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.38 + y0, width=80, height=30) B.place(relx=0.7, rely=0.38 + y0, width=80, height=30) lab1 = tk.Label(frame_right, text="链接时间", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if cur == -1: text = "无" else: text = str(d.Edges[cur][7]) lab2 = tk.Label(frame_right, text=f"{text}", bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) B = tk.Button(frame_right, text="设置时间", command=lambda n=cur: self.newedge_retime(n)) lab1.place(relx=0.1, rely=0.43 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.43 + y0, width=80, height=30) B.place(relx=0.7, rely=0.43 + y0, width=80, height=30) B = tk.Button(frame_right, text="删除一条连接", command=lambda: self.newedge_del(3, cur1, cur2)) B.place(relx=0.1, rely=0.5 + y0, width=150, height=30) B = tk.Button(frame_right, text="删除多余连接", command=lambda: self.newedge_del_all(1)) B.place(relx=0.1, rely=0.55 + y0, width=150, height=30) def edge_merge_cost(self, n1, n2): cur1 = node_edge(n1, n2)# 初始化三条可能的边的编号 cur2 = node_edge(n1, n2, 2)# 初始化三条可能的边的编号 cur3 = node_edge(n1, n2, 3)# 初始化三条可能的边的编号 edges = [(cur1, d.Edges[cur1][6]), (cur2, d.Edges[cur2][6]), (cur3, d.Edges[cur3][6])]#提出编号与路径花费 valid_edges = [(edge, cost) for edge, cost in edges if d.Edges[edge][3] == 1]# 过滤出存在的边 valid_edges.sort(key=lambda x: x[1])# 按照 cost 排序,保留 cost 最小的边 for idx, (edge, _) in enumerate(valid_edges): d.Edges[edge][8] = idx + 1 # 设置边的优先级 d.Edges[edge][0] = edge # 确保边的索引正确 for edge, _ in valid_edges[1:]:# 对于非最优边,设置其为不显示 d.Edges[edge][3] = 0 # def edge_merge_cost(self, n1, n2): # # 保留cost最小的一条,其他路径都不显示 # cur1 = G.node_edge(n1, n2) # cur2 = self.node_edge(2, n1, n2) # cur3 = self.node_edge(3, n1, n2) # # print(d.Edges[cur1]) # # print(d.Edges[cur2]) # # print(d.Edges[cur3]) # if d.Edges[cur2][3] == 1 and d.Edges[cur2][6] < d.Edges[cur1][6]: # e = d.Edges[cur1].copy() # d.Edges[cur1] = d.Edges[cur2].copy() # d.Edges[cur2] = e.copy() # d.Edges[cur1][8] = 1 # d.Edges[cur2][8] = 2 # if d.Edges[cur2][3] == 1 and d.Edges[cur3][6] < d.Edges[cur1][6]: # e = d.Edges[cur1].copy() # d.Edges[cur1] = d.Edges[cur3].copy() # d.Edges[cur3] = e.copy() # d.Edges[cur1][8] = 1 # d.Edges[cur3][8] = 3 # if d.Edges[cur2][3] == 1 and d.Edges[cur3][3] == 1 and d.Edges[cur3][6] < d.Edges[cur2][6]: # e = d.Edges[cur2].copy() # d.Edges[cur2] = d.Edges[cur3].copy() # d.Edges[cur3] = e.copy() # d.Edges[cur2][8] = 2 # d.Edges[cur3][8] = 3 # d.Edges[cur2][3] = 0 # d.Edges[cur3][3] = 0 # d.Edges[cur1][0] = cur1 # d.Edges[cur2][0] = cur2 # d.Edges[cur3][0] = cur3 def edge_merge_time(self, n1, n2): cur1 = node_edge(n1, n2)# 初始化三条可能的边的编号 cur2 = node_edge(n1, n2, 2)# 初始化三条可能的边的编号 cur3 = node_edge(n1, n2, 3)# 初始化三条可能的边的编号 edges = [(cur1, d.Edges[cur1][7]), (cur2, d.Edges[cur2][7]), (cur3, d.Edges[cur3][7])]#提出编号与路径花费 valid_edges = [(edge, cost) for edge, cost in edges if d.Edges[edge][3] == 1]# 过滤出存在的边 valid_edges.sort(key=lambda x: x[1])# 按照 cost 排序,保留 cost 最小的边 for idx, (edge, _) in enumerate(valid_edges): d.Edges[edge][8] = idx + 1 # 设置边的优先级 d.Edges[edge][0] = edge # 确保边的索引正确 for edge, _ in valid_edges[1:]:# 对于非最优边,设置其为不显示 d.Edges[edge][3] = 0 def edge_del_all(self, mode): # mode = 1:优先最短路径 # mode = 2:优先最短时间 # cur1 = node_edge() if mode == 1: for e in d.Edges: if (e[8] == 2 and e[3] == 1) or (e[8] == 3 and e[3] == 1): self.edge_merge_cost(e[1], e[2]) else: for e in d.Edges: if (e[8] == 2 and e[3] == 1) or (e[8] == 3 and e[3] == 1): self.edge_merge_time(e[1], e[2]) G.draw() def newedge_del_all(self, mode): self.edge_del_all(mode) self.edge_mark_display() def newedge_recost(self, no): edge = d.Edges[no] if not edge: tk.messagebox.askokcancel(title='错误', message='链接不合法') result = tkinter.simpledialog.askinteger(title='修改连接花费', prompt='请输入修改后的时间:', initialvalue=edge[7]) if result: edge[6] = result G.draw() self.edge_mark_display() def newedge_retime(self, no): edge = d.Edges[no] if not edge: tk.messagebox.askokcancel(title='错误', message='链接不合法') result = tkinter.simpledialog.askinteger(title='修改连接时间', prompt='请输入修改后的时间:', initialvalue=edge[7]) if result: edge[7] = result G.draw() self.edge_mark_display() def newedge_rename(self, no): edge = d.Edges[no] if not edge: tk.messagebox.askokcancel(title='错误', message='链接不合法') result = tkinter.simpledialog.askstring(title='修改连接标签', prompt='请输入修改后的标签:', initialvalue=edge[5]) if result: edge[5] = result G.draw() self.edge_mark_display() def newedge_add(self, no, na, nb): e = self.node_edge(no, na, nb) d.Edges[e][3] = 1 G.draw() self.edge_mark_display() def newedge_del(self, no, na, nb): e = self.node_edge(no, na, nb) d.Edges[e][3] = 0 # print(d.Edges[e]) G.draw() self.edge_mark_display() def node_mark1(self, name): if name >= len(d.Nodes): N.node_404(name) return for node in d.Nodes: if node[0] == name: node[2] = 1 elif node[2] == 1: node[2] = 0 G.draw() self.edge_mark_display() def node_mark2(self, name): if name >= len(d.Nodes): N.node_404(name) return for node in d.Nodes: if node[0] == name: node[2] = 2 elif node[2] == 2: node[2] = 0 G.draw() self.edge_mark_display() def edge_add(self): for edge in d.Edges: if d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: if edge[8] == 1 and edge[3] == 1: tk.messagebox.askokcancel(title='错误', message='已存在连线') return else: edge[3] = 1 G.draw() self.edge_mark_display() return tk.messagebox.askokcancel(title='错误', message='请选择两个节点') def edge_del(self): for edge in d.Edges: if d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: edge[3] = 0 G.draw() self.edge_mark_display() return tk.messagebox.askokcancel(title='错误', message='请选择两个节点') def edge_delall(self): for edge in d.Edges: edge[3] = 0 G.draw() self.edge_mark_display() def edge_addall(self): for edge in d.Edges : if edge[8] == 1: edge[3] = 1 G.draw() self.edge_mark_display() def edge_rename(self): for edge in d.Edges: if d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: if edge[3] == 0: tk.messagebox.askokcancel(title='错误', message='不存在连线') return else: result = tkinter.simpledialog.askstring(title='修改连接标签', prompt='请输入修改后的标签:', initialvalue=edge[5]) if(result): edge[5] = result G.draw() self.edge_mark_display() return tk.messagebox.askokcancel(title='错误', message='请选择两个节点') def edge_recost(self): for edge in d.Edges: if d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: if edge[3] == 0: tk.messagebox.askokcancel(title='错误', message='不存在连线') return else: result = tkinter.simpledialog.askinteger(title='修改连接成本', prompt='请输入修改后的成本:', initialvalue=edge[6]) if result: edge[6] = result G.draw() self.edge_mark_display() return tk.messagebox.askokcancel(title='错误', message='请选择两个节点') def edge_retime(self): for edge in d.Edges: if d.Nodes[edge[1]][2] + d.Nodes[edge[2]][2] == 3: if edge[3] == 0: tk.messagebox.askokcancel(title='错误', message='不存在连线') return else: result = tkinter.simpledialog.askinteger(title='修改连接时间', prompt='请输入修改后的时间:', initialvalue=edge[7]) if result: edge[7] = result G.draw() self.edge_mark_display() return tk.messagebox.askokcancel(title='错误', message='请选择两个节点') def edge_mark(self): for i in d.Edges: if d.Nodes[i[1]][2] == True and d.Nodes[i[2]][2] == True: i[4] = True def edge_nmark(self): for i in d.Edges: if d.Nodes[i[1]][2] == True and d.Nodes[i[2]][2] == True: i[4] = False def edge_clear(self): for edge in d.Edges: edge[4] = False G.draw() if __name__ == '__main__': G.draw() E = Edge() E.edge_mark_display() mainloop()