简体   繁体   English

在Tkinter中显示matplotlib

[英]Displaying matplotlib inside Tkinter

I have matplotlib script that reads an Excel file and draws it. 我有matplotlib脚本,可以读取Excel文件并进行绘制。 (as MATPLOT) (作为MATPLOT)

And i made another python script looks like a normal program (topmenu, statusbar..) (as GUI) 然后我使另一个python脚本看起来像普通程序(topmenu,statusbar ..)(作为GUI)

I want to display MATPLOT in my GUI. 我想在我的GUI中显示MATPLOT。 Is there any way to call all of the script inside the MATPLOT to run in GUI? 有什么方法可以调用MATPLOT内部的所有脚本以在GUI中运行? Like embedding a video to another website. 就像将视频嵌入到另一个网站一样。

root = Tk()
root.geometry("800x600")

#MENU
topMenu = Menu(root)
root.config(menu=topMenu)

loadMenu = Menu(topMenu, tearoff=0)
topMenu.add_cascade(label="File", menu=loadMenu)
loadMenu.add_command(label="Import New", command=doNothing)
loadMenu.add_command(label="Show 'Filter' Menu" , command=showOptions)
loadMenu.add_separator()
loadMenu.add_command(label="Exit", command=root.quit)

#GRAPH
#

#StatusBar
status = Label(root, text="File Name:", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

root.mainloop()

How to embed a matplotlib Figure in a tkinter Frame 如何在tkinter框架中嵌入matplotlib图

The approach presented subclasses tk.Frame and matplotlib.figure.Figure to make it easy to re-use the code for other purposes. 该方法提供了tk.Framematplotlib.figure.Figure子类,以便于将代码轻松用于其他目的。

It creates a tkFrame , with all the boilerplate ready to accept and display a matplotlib Figure . 它创建一个tkFrame ,所有样板都准备好接受并显示matplotlib Figure
It then creates a Figure (again, ready to be modified and expanded), and embeds it in the tk.Frame created above. 然后,它创建一个Figure (再次准备好进行修改和扩展),并将其嵌入到tk.Frame创建的tk.Frame
Last, it launches the tk.mainloop 最后,它启动tk.mainloop

import matplotlib
import tkinter as tk

matplotlib.use('TkAgg')
# from matplotlib import style
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure


class GraphPage(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.title_label = tk.Label(self, text="Graph Page Example")
        self.title_label.pack()
        self.pack()

    def add_mpl_figure(self, fig):
        self.mpl_canvas = FigureCanvasTkAgg(fig, self)
        self.mpl_canvas.show()
        self.mpl_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        self.toolbar = NavigationToolbar2TkAgg(self.mpl_canvas, self)
        self.toolbar.update()
        self.mpl_canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


class MPLGraph(Figure):

    def __init__(self):
        Figure.__init__(self, figsize=(5, 5), dpi=100)
        self.plot = self.add_subplot(111)
        self.plot.plot([1, 2, 3, 4, 5, 6, 7], [4, 3, 5, 0, 2, 0, 6])


fig = MPLGraph()

root = tk.Tk()
graph_page = GraphPage(root)
graph_page.add_mpl_figure(fig)

root.mainloop()

matplotlib图嵌入在tkinter框架中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM