Python Tkinter 在 GUI 中嵌入 Matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4073660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:07:43  来源:igfitidea点击:

Python Tkinter Embed Matplotlib in GUI

pythoncanvasgridmatplotlibtkinter

提问by thenickname

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?

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

#!/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()

采纳答案by Bryan Oakley

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).

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

However, you can still add other widgets. packworks 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:

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

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

Notice that if you put this code afterthe code where you packthe 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 TOPagain it means "at the top of the area below whatever is already at the top".

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

So, you have some choices. The best choice is to make your widgets in the order you wish them to appear. If you packthe toolbar at the top before you packthe 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.

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

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.

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

Another choice which may fit your mental model better is to gridinstead ofpack. With gridyou 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.

另一个可能更适合您的心智模型的选择是grid代替pack。有了grid你可以选择行(S)和列(s)表示,小部件占据。这使得在网格中布置东西变得容易,但代价是不得不使用更多的代码。

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)

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.

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

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

欲了解更多信息,请参阅此页面上的网格,并在包装这个页面