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

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

问题描述

我试图在我的Tkinter GUI中嵌入一个绘图,用Python编码。我相信下面的代码成功地简单地将图形放入画布,但我没有在GUI网格中的画布位置的任何控制。我想要能够有我的GUI的一个部分是情节...不是它的全部。如何定位此画布窗口小部件?

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

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

f =图(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)

b $ b 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 )。



但是,您仍然可以添加其他小部件。 pack 通过将小部件放在容器的一侧来工作。您的容器 master 始终有四个边。例如,如果你想创建一个工具栏,你可以这样做:

  toolbar = tk.Frame 
button = tk.Button(toolbar,text =Push me)
button.pack(side =left)#父对象的左侧,工具栏框架
toolbar.pack侧面= TOP,fill =x)#父窗口顶部,主窗口

你把这个代码放在 pack 后的代码之后,工具栏就会显示在底部!这是因为 TOP BOTTOM 等指的是任何其他已经小于$ c $的小部件c> pack ed。剧情占据了顶部,剩下的空间在底部。因此,当您再次指定 TOP 时,意味着在顶部已经位于顶部的区域顶部。



所以,你有一些选择。最好的选择是按照你希望他们出现的顺序来创建你的小部件。如果 pack pack 之前的顶部工具栏,它将是显示在非常顶。此外,你可以把底部而不是顶部的情节,这也将解决问题。



顺便说一句,我通常创建我的小部件在一个块,然后把它们全部放在一个单独的块。



另一个可能适合你的心理模型的选择是更好的是 grid 而不是 pack 。使用 grid ,您可以选择窗口小部件占用的行和列。这使得它很容易在网格中布局,但是代价是不得不使用更多的代码。



例如,要将工具栏放在顶部,可以执行以下操作:

  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)

请注意,行和列从零开始。此外,权重是指此窗口小部件相对于其他窗口小部件的扩展量。有两行相等的权重,当窗口调整大小时,它们将平等扩展。权重为零意味着无膨胀。



有关更多信息,请参阅网页上的此网页此网页on pack a>。


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

解决方案

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

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

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

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.

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)

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天全站免登陆