通过网格在 Tkinter 中显示 Matplotlib 导航工具栏 [英] Displaying Matplotlib Navigation Toolbar in Tkinter via grid
问题描述
我正在开发一个小型 Tkinter GUI 来绘制 matplotlib-plots.(它包含一些条目并根据其内容组装情节.)
I'm developing a small Tkinter GUI to draw matplotlib-plots. (It contains a few Entries and assembles the plot according to their content.)
我根据 http://matplotlib.org/examples/user_interfaces/设计了我的绘图小部件embedding_in_tk.html,只有我用grid而不是pack:
I have designed my plotting widget according to http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html, only I use grid instead of pack:
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=1,column=4,columnspan=3,rowspan=20)
那部分有效.但是以同样的方式嵌入 NavigationToolbar 不会.当我包含以下行时,Tkinter 没有错误地崩溃:
That part works. But embedding the NavigationToolbar in the same fashion does not. Tkinter breaks down without error when I include the lines:
toolbar = NavigationToolbar2TkAgg( canvas, root )
canvas._tkcanvas.grid(row=22,column=4)
我知道这是因为 NavigationToolbar 在内部调用 pack
,而 pack
和 grid
相处不来.但是,我喜欢网格,并且不想为了能够使用 NavigationToolbar 而不得不重新设计我的整个 GUI.
I know this is because NavigationToolbar calls pack
internally, and pack
and grid
don't get along. However, I like grid and would hate to have to redesign my whole GUI just to be able to use the NavigationToolbar.
是否有解决方法,以便我可以通过网格使用 NavigationToolbar2TkAgg?(我找到了子类和重载"的建议 这里,但不知道怎么做.)
Is there a workaround so I can use NavigationToolbar2TkAgg via grid? (I have found the advice to "subclass and overload" here, but don't know how to do that.)
非常感谢任何帮助!
推荐答案
你能不能创建一个空框架,然后把 NavigationToolbar
放在那个框架里?我假设 NavigationToolbar
然后将自己打包在该框架中.然后您可以在框架上使用网格.
Can you create an empty frame, then put the NavigationToolbar
in that frame? I assume the NavigationToolbar
will then pack itself in that frame. You can then use grid on the frame.
这篇关于通过网格在 Tkinter 中显示 Matplotlib 导航工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!