# -*- encoding: utf-8 -*- """ @Author: packy945 @FileName: X3.py @DateTime: 2023/7/12 14:07 @SoftWare: PyCharm """ # -*- encoding: utf-8 -*- from head import * class Node: # 标记节点按钮 def node_mark_display(self): for w in frame_right.winfo_children(): w.destroy() # 清除右侧组件 curx = -1 y0 = 0.1 for i in d.Nodes: if i[2] == 1: curx = i[0] lab = tk.Label(frame_right, text="节点操作", bg=RED, fg=BLACK, font=('微软雅黑', 20)) lab.place(relx=0.18, rely=0.02, width=200, height=30) lab1 = tk.Label(frame_right, text=f"当前共有{len(d.Nodes)}个节点", bg=RED, fg=BLACK, font=('微软雅黑', 15)) lab1.place(relx=0.1, rely=0.0 + y0, width=200, height=30) lab1 = tk.Label(frame_right, text="当前节点:", bg=RED, fg=BLACK, font=('微软雅黑', 12)) if curx == -1: text = "未选择" else: text = f"{d.Nodes[curx][1]}" lab2 = tk.Label(frame_right, text=text, bg=BLUE, fg=BLACK, font=('微软雅黑', 12)) lab1.place(relx=0.1, rely=0.05 + y0, width=80, height=30) lab2.place(relx=0.4, rely=0.05 + y0, width=80, height=30) B = tk.Button(frame_right, text="设置标签", command=lambda: self.node_rename()) B.place(relx=0.7, rely=0.05 + y0, width=80, height=30) B = tk.Button(frame_right, text="删除当前节点", command=lambda: self.node_del_exact()) B.place(relx=0.1, rely=0.1 + y0, width=150, height=30) if d.Nodes[curx][4] == 1: text = '设为非必经节点' else: text = '设为必经节点' B = tk.Button(frame_right, text=text, command=lambda: self.dominator(curx)) B.place(relx=0.1, rely=0.15 + y0, width=150, height=30) B = tk.Button(frame_right, text="增加一个节点", command=lambda: self.node_add()) B.place(relx=0.1, rely=0.25 + y0, width=150, height=30) B = tk.Button(frame_right, text="减少一个节点", command=lambda: self.node_del()) B.place(relx=0.1, rely=0.3 + y0, width=150, height=30) def dominator(self, x): d.Nodes[x][4] ^= 1 G.draw() self.node_mark_display() def node_mark(self, name): if name >= len(d.Nodes): self.node_404(name) return for node in d.Nodes: if node[0] == name: node[2] = 1 else: node[2] = 0 G.draw() self.node_mark_display() def node_del_exact(self): if d.nodes_num <= 2:# 若删除节点以后节点过少,则直接返回并警告 tk.messagebox.askokcancel(title='错误', message='节点过少') return flag = 0 cur = -1 for node in d.Nodes:# 寻找当前选定的节点 if node[2] == 1: cur = node[0] flag = 1 if flag == 0: tk.messagebox.askokcancel(title='错误', message='未选择节点') return d.nodes_num -= 1#节点数目减一 d.ang = 360 / d.nodes_num#重新计算圆心角 nodes = d.Nodes.copy()#复制节点 d.Nodes.clear()#清除Data中的节点 num = 0 for n in nodes:#逐个复制节点 if n[0] == cur:#若是删除的节点,则不复制 continue n[0] = num#重新编号 rad = math.radians(num * d.ang)#计算当前角度 x = int(math.cos(rad) * d.R)#计算当前点x坐标(相对) y = int(math.sin(rad) * d.R)#计算当前点y坐标(相对) n[3] = (d.center[0] + x, d.center[1] + y)#计算绝对坐标 d.Nodes.append(n)#将当前点加入点集合中 num += 1 edges = d.Edges.copy()#将原有边集复制出来 d.Edges.clear()#删除Data中的边集 num = 0 for e in edges:#复制边 if e[1] == cur or e[2] == cur:#删除的边不复制 continue if e[1] > cur:#修改边对应的点的编号 e[1] -= 1 if e[2] > cur:#修改边对应的点的编号 e[2] -= 1 e[0] = num#重新编号 d.Edges.append(e)#将边加入边集中 num += 1 G.draw()#重新绘图 self.node_mark_display() def node_rename(self): flag = 0 for node in d.Nodes: if node[2] == 1: cur = node flag = 1 if flag == 1: result = tkinter.simpledialog.askstring(title='修改节点标签', prompt='请输入修改后的标签:', initialvalue=cur[1]) else: tk.messagebox.askokcancel(title='错误', message='未选择节点') return if result: cur[1] = result G.draw() self.node_mark_display() def node_clear(self): for node in d.Nodes: node[2] = False G.draw() def node_add(self): # 增加一个节点 nodes = len(d.Nodes) + 1 # 如果节点数目大于五,则将连接详细信息改为不显示 if nodes > 5: d.edgeinfo = 0 self.node_init(nodes) G.draw() self.node_mark_display() def node_set(self): node = tkinter.simpledialog.askinteger(title='修改节点数目', prompt='请输入修改后的节点数目:', initialvalue=str(len(d.Nodes))) if node: self.node_init(node) G.draw() def node_del(self): if d.nodes_num <= 2: tk.messagebox.askokcancel(title='错误', message='节点过少') return nodes = len(d.Nodes) - 1 if nodes < 6: d.edgeinfo = 1 self.node_init(nodes) G.draw() self.node_mark_display() def node_init(self, node): if not node: return if d.source == 1: d.data(node) elif d.source == 2: if node > 13: tk.messagebox.askokcancel(title='错误', message='节点过多') return d.HEI_data(node) else: if node > 13: tk.messagebox.askokcancel(title='错误', message='节点过多') return d.LIAO_data(node) if node > 5: d.edgeinfo = 0 else: d.edgeinfo = 1 def graph_del(self): global d del d def node_refresh(self): nodes = len(d.Nodes) self.node_init(nodes) G.draw() self.node_mark_display() def node_404(self, name): # 弹出对话框 tk.messagebox.askokcancel(title='错误', message='该节点不存在') # print(result) if __name__ == '__main__': G.draw() node = Node() node.node_mark_display() mainloop()