SoFunction
Updated on 2024-11-18

Step-by-step details of python embedding matplotlib into tkinter

matplotlib window composition

tkinter is Python standard library comes with GUI tools, very easy to use, such as matplotlib can be embedded into tkinter, you can make a relatively professional data display system, very competitive.

Before getting into the specifics of implementation, it is useful to look at a typicalmatplotlib computer operating system window

import numpy as np
import  as plt
((100))
()

And then we see the familiar plot diagrams

plot图

The diagram consists of two parts, the FigureCanvasTkAgg canvas for drawing on top, and the NavigationToolbar2Tk toolbar on the bottom, both of which are stored in .backend_tkagg, and both of which are equivalent in status to the components in tkinter.

But this is not enough, because the canvas does not mean that there is an image, so you also have to import the drawing window Figure, so embedding matplotlib into tkinter requires at least the following modules

import tkinter as tk
import  as ttk
import matplotlib as mpl
('TkAgg')        # Enable tkinter to render matplotlib so it can be embedded into tkinter
import  as plt
from .backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from  import Figure
import numpy as np

Where ('TkAgg') is used to enable tkinter to render matplotlib so that it can be embedded into tkinter.

tkinter layout

After understanding the principle, the specific implementation is not difficult. Before embedding matplotlib, you can first do the layout of the tkinter window, the code is shown below, first of all, create a title for the "data display tool" window root, and add two controls for it, respectively, the right side of the frmCtrl and the left side of the frmFigure, the former is used to add controls, the latter is used for the embedded image!

root = ()
("Data presentation tools")
frmCtrl = (root, width=200)
(side=)
frmFigure = (root)
(side=, fill=, expand=)

Embedded images

As mentioned at the beginning, matplotlib's drawing window consists of two parts, the drawing canvas and the toolbar, and the drawing canvas has to contain the image, so first create a Figure, and then import it into FigureCanvasTkAgg.

fig = Figure()
canvas = FigureCanvasTkAgg(fig,frmFigure)
canvas.get_tk_widget().pack(
    side=,fill=,expand=)
toolbar = NavigationToolbar2Tk(canvas,frmFigure,
    pack_toolbar=False)
()
(side=)

Where canvas is the drawing canvas and toolbar is the toolbar. At this point, the embedding of the image has been completed, and the next thing to do is to run a dead loop so that the window with the embedded drawing tool can be displayed.

()

The results are as follows

在这里插入图片描述

The left side of this figure is obviously the drawing canvas introduced by matplotlib, and the right side is frmCtrl, mainly to demonstrate more clearly that the canvas is indeed embedded in the tkinter window.

If you don't feel like it, you can draw an image on it after creating the fig, with the following code

ax = fig.add_subplot()
((100))

Then execute () again to get the following image

在这里插入图片描述

to this article on python will matplotlib embedded in tkinter step by step details of the article is introduced to this, more related to python will matplotlib embedded in tkinter content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!