Python Tkinter 在 GUI 中嵌入 Matplotlib [英] Python Tkinter Embed Matplotlib in GUI

查看:48
本文介绍了Python Tkinter 在 GUI 中嵌入 Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我用 Python 编码的 Tkinter GUI 中嵌入一个绘图.我相信下面的代码可以成功地将图形简单地放入画布中,但我无法控制 GUI 网格中的画布位置.我希望能够将我的 GUI 的一部分作为情节......而不是全部.如何定位此画布小部件?

I'm trying to embed a plot in my Tkinter GUI coded in Python. I believe the code below succeeds in simply putting a graph into a canvas, but I don't have any control of the canvas location within the GUI grid. I want to be able to have a subsection of my GUI be the plot...not the entirety of it. How can I position this canvas widget?

#!/usr/apps/Python/bin/python
import matplotlib, sys
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from Tkinter import *

master = Tk()
master.title("Hello World!")
#-------------------------------------------------------------------------------

f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t,s)


dataPlot = FigureCanvasTkAgg(f, master=master)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
#-------------------------------------------------------------------------------
master.mainloop()

推荐答案

您没有任何其他小部件,因此很难知道您想要其他小部件的位置.不过,我可以告诉您的是:通过执行 dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) 您要求 Tkinter 用绘图填充屏幕.这是因为您要求它填充所有方向(fill=BOTH)并扩展以填充任何额外的空间(expand=1).

You don't have any other widgets so it's hard to know where you want other widgets. Here's what I can tell you though: by doing dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1) you are asking Tkinter to fill the screen with the plot. This, because you ask it to fill in all directions (fill=BOTH) and expand to fill any extra space (expand=1).

但是,您仍然可以添加其他小部件.pack 的工作原理是将小部件放在容器的一侧.你的容器,master,总是有四个面.因此,例如,如果您想创建一个工具栏,您可以执行以下操作:

However, you can still add other widgets. pack works by putting widgets on one side of a container. Your container, master, always has four sides. So, for example, if you wanted to create a toolbar you would do something like:

toolbar = tk.Frame(master)
button = tk.Button(toolbar, text="Push me")
button.pack(side="left") # left side of parent, the toolbar frame
toolbar.pack(side=TOP, fill="x") # top of parent, the master window

请注意,如果您将此代码放在pack绘图的代码之后,工具栏会显示在底部!这是因为TOPBOTTOM 等指的是任何其他已经pack 的小部件留下的空间.情节占据顶部,剩下的空间在底部.因此,当您再次指定 TOP 时,它的意思是在已经位于顶部的区域下方的顶部".

Notice that if you put this code after the code where you pack the plot, the toolbar shows up on the bottom! That's because TOP, BOTTOM, etc refer to space left over by any other widgets that have already been packed. The plot takes up the top, the space left over is at the bottom. So when you specify TOP again it means "at the top of the area below whatever is already at the top".

所以,你有一些选择.最好的选择是按照您希望它们出现的顺序制作小部件.如果您在pack绘图之前pack顶部的工具栏,它将是显示在最顶部的工具栏.此外,您可以将图放在底部而不是顶部,这样也可以解决问题.

So, you have some choices. The best choice is to make your widgets in the order you wish them to appear. If you pack the toolbar at the top before you pack the plot, it will be the toolbar that shows up at the very top. Further, you can place the plot at the bottom rather than the top and that will solve the problem, too.

顺便说一下,我通常在一个块中创建我的小部件,然后将它们全部放在一个单独的块中.我发现它使代码更易于维护.

By the way, I typically create my widgets in one block, then lay them all out in a separate block. I find it makes the code easier to maintain.

另一个可能更适合您的心智模型的选择是grid 而不是 pack.使用 grid 您可以选择小部件占据的行和列.这使得在网格中布置东西变得容易,但代价是不得不使用更多的代码.

Another choice which may fit your mental model better is to grid instead of pack. With grid you can choose the row(s) and column(s) that the widget occupies. This makes it easy to lay things out in a grid, but at the expense of having to use a little more code.

例如,将工具栏放在顶部,将绘图放在下面,您可以这样做:

For example, to put the toolbar at the top and the plot down below you might do:

toolbar.grid(row=1, column=1, sticky="ew")
dataPlot.get_tk_widget().grid(row=1, column=1, sticky="nsew")
master.grid_rowconfigure(0, weight=0)
master.grid_rowconfigure(1, weight=1)
master.grid_columnconfigure(0, weight=1)

注意行和列从零开始.此外,权重"是指此小部件相对于其他小部件的扩展程度.两行重量相等,当窗口调整大小时,它们将相等地扩展.权重为零意味着没有扩展.一行的权重为 2,另一行的权重为 1 表示前者的权重是后者的两倍.

Notice that rows and columns start at zero. Also, "weight" refers to how much this widget expands relative to other widgets. With two rows of equal weight, they will expand equally when the window is resized. A weight of zero means no expansion. A weight of 2 for one row, and 1 for another means that the former will expand twice as much as the latter.

有关更多信息,请参阅网格上的此页面此页面包.

For more information see this page on grid, and this page on pack.

这篇关于Python Tkinter 在 GUI 中嵌入 Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆