📜  如何在 Tkinter GUI 中嵌入 Matplotlib 图表?

📅  最后修改于: 2022-05-13 01:54:35.189000             🧑  作者: Mango

如何在 Tkinter GUI 中嵌入 Matplotlib 图表?

先决条件: Tkinter 简介| Matplotlib 简介

当从Python shell 使用 Matplotlib 时,绘图显示在默认窗口中。这些图可以嵌入到许多图形用户界面中,例如 wxpython、pygtk 或 Tkinter。这些可用作输出图目标的各种选项称为“后端”。 matplotlib.backend中有各种模块可用于选择后端。一个这样的模块是backend_tkagg ,它对于在Tkinter中嵌入绘图很有用。

创建 Tkinter 应用程序:

首先,让我们创建一个带有主窗口和一个可用于显示绘图的按钮的基本 Tkinter 应用程序。

Python3
# import all classes/methods
# from the tkinter module
from tkinter import *
  
# The main tkinter window
window = Tk()
  
# setting the title and 
window.title('Plotting in Tkinter')
  
# setting the dimensions of 
# the main window
window.geometry("500x500")
  
# button that would displays the plot
plot_button = Button(master = window,
                     height = 2,
                     width = 10,
                    text = "Plot")
# place the button
# into the window
plot_button.pack()
  
# run the gui
window.mainloop()


Python3
from tkinter import * 
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)
  
# plot function is created for 
# plotting the graph in 
# tkinter window
def plot():
  
    # the figure that will contain the plot
    fig = Figure(figsize = (5, 5),
                 dpi = 100)
  
    # list of squares
    y = [i**2 for i in range(101)]
  
    # adding the subplot
    plot1 = fig.add_subplot(111)
  
    # plotting the graph
    plot1.plot(y)
  
    # creating the Tkinter canvas
    # containing the Matplotlib figure
    canvas = FigureCanvasTkAgg(fig,
                               master = window)  
    canvas.draw()
  
    # placing the canvas on the Tkinter window
    canvas.get_tk_widget().pack()
  
    # creating the Matplotlib toolbar
    toolbar = NavigationToolbar2Tk(canvas,
                                   window)
    toolbar.update()
  
    # placing the toolbar on the Tkinter window
    canvas.get_tk_widget().pack()
  
# the main Tkinter window
window = Tk()
  
# setting the title 
window.title('Plotting in Tkinter')
  
# dimensions of the main window
window.geometry("500x500")
  
# button that displays the plot
plot_button = Button(master = window, 
                     command = plot,
                     height = 2, 
                     width = 10,
                     text = "Plot")
  
# place the button 
# in main window
plot_button.pack()
  
# run the gui
window.mainloop()



输出 :

tkinter 简单窗口

嵌入情节:

首先,我们需要使用Figure()类创建图形对象。然后,使用FigureCanvasTkAgg()类创建Tkinter画布(包含图形)。 Matplotlib 图表底部默认有一个工具栏。但是,在使用Tkinter时,需要使用NavigationToolbar2Tk()类将这个工具栏单独嵌入到画布中。
在下面的实现中,一个简单的图表:

y = x^2

被绘制。 plot函数绑定到一个按钮,该按钮在按下时显示图形。

Python3

from tkinter import * 
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)
  
# plot function is created for 
# plotting the graph in 
# tkinter window
def plot():
  
    # the figure that will contain the plot
    fig = Figure(figsize = (5, 5),
                 dpi = 100)
  
    # list of squares
    y = [i**2 for i in range(101)]
  
    # adding the subplot
    plot1 = fig.add_subplot(111)
  
    # plotting the graph
    plot1.plot(y)
  
    # creating the Tkinter canvas
    # containing the Matplotlib figure
    canvas = FigureCanvasTkAgg(fig,
                               master = window)  
    canvas.draw()
  
    # placing the canvas on the Tkinter window
    canvas.get_tk_widget().pack()
  
    # creating the Matplotlib toolbar
    toolbar = NavigationToolbar2Tk(canvas,
                                   window)
    toolbar.update()
  
    # placing the toolbar on the Tkinter window
    canvas.get_tk_widget().pack()
  
# the main Tkinter window
window = Tk()
  
# setting the title 
window.title('Plotting in Tkinter')
  
# dimensions of the main window
window.geometry("500x500")
  
# button that displays the plot
plot_button = Button(master = window, 
                     command = plot,
                     height = 2, 
                     width = 10,
                     text = "Plot")
  
# place the button 
# in main window
plot_button.pack()
  
# run the gui
window.mainloop()

输出 :

tkinter 与情节