[英]Opening NetworkX Viewer from an existing Tkinter window
I have a sample application which contains a main window with a simple button "Draw Graph in a new window". 我有一个示例应用程序,其中包含一个带有简单按钮“在新窗口中绘制图形”的主窗口。 On clicking this button, it should take me to a child window and draw a graph there.
单击此按钮后,应该带我到一个子窗口并在那里绘制图形。 Instead, the graph is drawn in the main window, NetworkX viewer is opened in a new (third) window and the child window is blank.
而是在主窗口中绘制图形,在新的(第三个)窗口中打开NetworkX查看器,子窗口为空白。
import Tkinter as tk
import networkx as nx
from networkx_viewer import Viewer
class MainWindow(tk.Frame):
counter = 0
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.button = tk.Button(self, text="Draw Graph in a new window",
command=self.create_window)
self.button.pack(side="top")
def create_window(self):
self.counter += 1
t = tk.Toplevel(self)
t.wm_title("Window #%s" % self.counter)
l = tk.Label(t, text="This is window #%s" % self.counter)
l.pack(side="top", fill="both", expand=True, padx=100, pady=100)
G=nx.complete_graph(30)
G.add_edge('a','b')
G.add_edge('b','c')
G.add_edge('c','a')
G.add_edge('c','d')
G.add_edge('b','d')
G.add_edge('p','q')
G.add_edge('q','r')
G.add_edge('r','p')
G.add_edge('r','s')
G.add_edge('q','s')
G.add_edge('w','x')
G.add_edge('x','y')
G.add_edge('y','w')
G.add_edge('y','z')
G.add_edge('x','z')
app = Viewer(G)
app.mainloop()
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
I've seen this before and it is befuddling. 我以前见过,这令人困惑。 It's not been directly resolved, but if you really need to use tkinter to have a separate parent GUI, the only fix I've seen is to use another displaying solution.
尚未直接解决,但如果您确实需要使用tkinter来拥有单独的父GUI,则我所见的唯一解决方法是使用其他显示解决方案。 Matplotlib's pyplot works well with networkx, and a tkinter menu can spawn the display with no issues.
Matplotlib的pyplot可与networkx配合使用,并且tkinter菜单可以毫无问题地生成显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.