【丁丁历险记】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的版本中测试通过

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