How to use matplotlib to draw graphics in tkinter with Python
This article mainly introduces the relevant knowledge of "how to use Python to draw graphics in tkinter". The editor shows you the process of operation through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to achieve Python in tkinter using matplotlib to draw graphics" can help you solve the problem.
one。 Code:
# coding=utf-8
Importsys
ImportTkinterasTk
Importmatplotlib
Fromnumpyimportarange,sin,pi
From Matt Lotley. Back end. Backend _ Tkaggimportfigures extensible tag, Navigation Toolbar 2 tag
Frommatplotlib . Backend _ basesimportkey_press_handler
From Matt Lotley. Diagram import diagram
Matplotlib.use ('TkAgg')
Root=Tk .TK ()
Huawei Cloud Test-matplotlibinTK')
# set drawing size and quality
F = number (figsize= (5) 4), dpi=100)
A=f.add_subplot (111)
T=arange (0. 0d3. 0. 01)
S=sin (2*pi*t)
# drawing graphics
A. Drawing (tmeme s)
# display the drawn graphics on the graphical interface window
CanvastKaggg (fjore mastery root)
Canvas.show ()
Canvas.get_tk_widget (). Packing (side = Tk. Top, fill=Tk. Both, expand = 1)
# display the navigation toolbar with beautifully drawn charts and graphs to the graphical interface window
Toolbar = Navigation Toolbar 2 label (canvas, root)
Toolbar. Update ()
Canvas. Tkcanvas.pack (side=Tk. Top, fill=Tk. Both, expand = 1)
# define and bind keyboard event handlers
Defon_key_event (event):
Print ('you pressed% s'% event. Key ")
Key_press_handler (event, canvas, toolbar)
Canvas. Mpl _ connect ('key_ press _ event', on_key_event)
# Button Click event handler
Def_quit ():
# end the main loop of the event and destroy the application window
Root.quit ()
Root.destroy ()
Button=Tk. Button (main = root, text = 'exit, command = _ exit)
Button.pack (side=Tk. Bottom)
Main circuit (II. Running result:)
This is the end of the introduction on "how to use Python to draw graphics with matplotlib in tkinter". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.