[Python][Voice]Integrate a plot of matplotlib into tkinter GUI/整合matplotlib的繪圖到 tkinter圖形視窗中
This example shows how to integrate a plot or figure on matplotlib into
the graphic user interface of tkinter.
1. Import tkinter
2. Import matplotlib
3. Import FigureCanvasTkAgg
4. Import Figure
5. Build a window named "win".
6. Window format 1: title()
7. Window format 2: geometry() determine size and initial position(x,y)
8. Window format 3: maxsize() set max size of a window
9. Build a figure with figsize = (Width: inch, Length: inch) and dpi = pixel per inch
10. Add the subplot to the figure.
11. Generate x array
12. Generate y array
13. Use plot() to draw the graph
14. Use FigureCanvasTkAgg(figure,win) to put figure on the win (window)
15. Use get_tk_widget().pack() to place figure on the win (window)
16. Build close_win function to end the window
17. Build Button component and give a string to text, and use command to call the function
18. Use pack to arrange Button on bottom side
19. Use mainloop() to keep window running for monitoring and handling events
這個範例說明如何整合matplotlib的繪圖到tkinter的圖形介面上。
We will share more tutorial videos with people who want to learn LabVIEW and Python welcome to subscribe this channel. 未來會陸續更新影片,讓想學習LabVIEW和Python的朋友可以看影片學習,歡迎訂閱此頻道。 其他網址/related websites: https://labview-tech.blogspot.com/ https://fishark.pixnet.net/blog email: jacklee3633@gmail.com Line: lvnet
#1. 引入tkinter (python 3)
#2. 引入matplotlib函式庫
#3. 引入FigureCanvasTkAgg: 將matplotlib的figure放在tk視窗上
#4. 引入Figure: Top level container for all plot elements. 將plot放在figure容器裡
#5 建立一個名為win的新視窗
#6. 視窗格式: 標題 Title()
#7. 視窗格式: 設定視窗啟動時的大小與位置 geometry("寬x長+x位移+y位移")
#8. 視窗格式: 設定視窗最大尺寸 maxsize(int長,int寬)
#9. 建立一個figure圖像,figsize=(寬度英吋,高度英吋);dpi=每英吋多少像素(https://matplotlib.org/api/figure_api.html)
#10. 新增一個subplot子圖到figure中
#11. 建立x陣列
#12. 建立y陣列
#13. 使用plot()方法繪圖
#14. 使用FigureCanvasTkAgg(figure,win)將figure加在win視窗上
#15. 使用get_tk_widget().pack()將figure在win視窗上做布局(fill=tk.X x方向填滿,fill=tk.Y x方向填滿,fill=tk.BOTH X、Y方向接填滿)
#16. 建立函數close_win函數: 結束win視窗
#17. 建立Button元件,按鍵文字 Text = ""
#18. Button元件布局 pack(side='bottom') 靠下
#19. mainloop()在使用者關閉視窗之前持續偵測視窗並處理事件
import tkinter as tk
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
win = tk.Tk()
win.title("tkinter plt")
win.geometry("700x650+10+10")
win.maxsize(1000,700)
#plot
figure=Figure(figsize=(3,3),dpi=100)
plot=figure.add_subplot(1,1,1)
x = [ 1, 2, 3 ]
y = [ -1, -2, -3 ]
plot.plot(x, y, color="blue", marker="x", linestyle=":")
canvas=FigureCanvasTkAgg(figure,win)
canvas.get_tk_widget().pack(side=tk.RIGHT, fill=tk.Y, expand=1) #fill=tk.BOTH/X/Y
#plot1
figure1=Figure(figsize=(3,3),dpi=100)
plot1=figure1.add_subplot(1,1,1)
x1 = [ 1, 2, 3 ]
y1 = [ 1, 2, 3 ]
plot1.plot(x1, y1, color="red", marker="o", linestyle="--")
canvas1=FigureCanvasTkAgg(figure1,win)
canvas1.get_tk_widget().pack(side=tk.LEFT, fill=tk.Y, expand=1) #fill=tk.BOTH/X/Y
def close_win():
win.destroy()
close_btn=tk.Button(win,text="Close",command=close_win)
close_btn.pack(side='bottom')
win.mainloop()
We will share more tutorial videos with people who want to learn LabVIEW and Python welcome to subscribe this channel. 未來會陸續更新影片,讓想學習LabVIEW和Python的朋友可以看影片學習,歡迎訂閱此頻道。 其他網址/related websites: https://labview-tech.blogspot.com/ https://fishark.pixnet.net/blog email: jacklee3633@gmail.com Line: lvnet
留言