【丁丁歷險記】Matplotlib+tkinter 圖表動態顯示上位機


  • 在進行製作的過程中常常需要對數據進行分析和操作
  • 本節主要使用matplotlib配合tkinter製作動態的
  • tkinter更多使用參數請參考 Python GUI tkinter

一、圖傳上位機界面設計

  • 在這裏通過將接收的數據以圖標的形式在tkinter中的進行動態的顯示。
  • Matlab的動態圖表需要canvas畫布控件 中進行動態顯示
  • 本文使用隨機數的方式生成數據,可按照需求進行替換
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
from tkinter import *


def video_loop():  #動態圖像現實窗口
    """
    動態matlib圖表
    """
    rand_data = abs(np.random.normal(1, 2, 12))
    f.clf()
    a = f.add_subplot(111)
    a.bar(range(12),abs(rand_data), align='center')
    a.set_title('title')
    canvas.draw()
    root.after(5, video_loop)


root = Tk()
root.title("Set title")
root.geometry('530x360')

"""
圖像畫布設置
"""
panel = Label(root)  # initialize image panel
panel.place(x=0,y=0,anchor='nw')
root.config(cursor="arrow")
"""
右側組件
"""
l1 = Label(root,text = "可以添加更多組件",bg='green',font=('Arial', 12), width=22, height=2)
l1.place(x=342,y=0,anchor='nw')

matplotlib.use('TkAgg')
f = Figure(figsize=(3,3), dpi=120)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw()
canvas.get_tk_widget().place(x=0,y=0 ,anchor='nw')

video_loop()
root.mainloop()

上述代碼在Python3.6的版本中測試通過

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章