在wxPython中实时更新Matlibpro

时间:2016-04-05 15:53:02

标签: python user-interface matplotlib wxpython

早上好/晚上好,

目前我正在编写一个与串行设备通信以接收电压和电流测量的程序。我将数据输出到wxPython中自己的文本框,并且我设置了一个计时器,以便只要程序打开就会不断轮询设备。我想更进一步,所以我决定在GUI中添加一个图形。

到目前为止,这就是我所拥有的:

class Slot1DetailFrame(wx.Frame):

      def __init__(self):
           """Constructor"""
           wx.Frame.__init__(self, None, title="Slot 1 Specifications")
           self.initialize()

           self.timer = wx.Timer(self)
           self.Bind(wx.EVT_TIMER, self.update, self.timer)
           self.timer.Start(2000)

      def initialize(self):

       sizer = wx.GridBagSizer(hgap = 5, vgap = 5)

       """ LABELS """
       self.VoltageLabel = wx.StaticText(self, -1, 'Voltage', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.VoltageLabel, (0,1))
       self.CurrentLabel = wx.StaticText(self, -1, 'Current', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.CurrentLabel, (0,2))
       self.PowerLabel = wx.StaticText(self, -1, 'Power', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.PowerLabel, (0,3))
       self.TotalPowerLabel = wx.StaticText(self, -1, 'Total Power', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.TotalPowerLabel, (4,2))


       """ RAIL 1 """
       self.Rail1Label = wx.StaticText(self, -1, 'Rail 1:', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.Rail1Label, (1,0))

       self.Rail1Voltage = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail1Voltage, (1,1),(1,1))

       self.Rail1Current = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail1Current, (1,2),(1,1))

       self.Rail1Power = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail1Power, (1, 3),(1,1))

       self.Rail1Config = wx.Button(self, -1,  "Configure")
       sizer.Add(self.Rail1Config,(1, 4), (1, 1))

       """ RAIL 2 """
       self.Rail2Label = wx.StaticText(self, -1, 'Rail 2:', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.Rail2Label, (2,0))

       self.Rail2Voltage = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail2Voltage, (2,1),(1,1))

       self.Rail2Current = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail2Current, (2,2),(1,1))

       self.Rail2Power = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail2Power, (2, 3),(1,1))

       self.Rail2Config = wx.Button(self, -1,  "Configure")
       sizer.Add(self.Rail2Config,(2, 4), (1, 1))

       """ RAIL 3 """
       self.Rail3Label = wx.StaticText(self, -1, 'Rail 3:', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.Rail3Label, (3,0))

       self.Rail3Voltage = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail3Voltage, (3,1),(1,1))       

       self.Rail3Current = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail3Current, (3,2),(1,1))

       self.Rail3Power = wx.TextCtrl(self,-1,'', wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
       sizer.Add(self.Rail3Power, (3,3),(1,1))   

       self.Rail3Config = wx.Button(self, -1,  "Configure")
       sizer.Add(self.Rail3Config,(3, 4), (1, 1))

       """ TOTAL POWER """
       self.TotalPower = wx.TextCtrl(self, -1, '', wx.DefaultPosition, wx.DefaultSize)
       sizer.Add(self.TotalPower, (4,3))

       """ TEMPERATURE GRAPH """
       x = 0
       y = 0
       self.fig = Figure(figsize=(3, 3), dpi=100)
       self.canvas = FigCanvas(self, -1, self.fig)
       self.ax = self.fig.add_subplot(111)
       self.ax.set_xlim(0,100)
       self.ax.set_ylim(0,100)
       line,= self.ax.plot(x,y,'-')

       # Add figure
       sizer.Add(self.canvas, (5,0), (5,5), wx.EXPAND)

       self.SetSizerAndFit(sizer)
       sizer.AddGrowableCol(0)
       sizer.AddGrowableCol(1)

      def update(self, event):

       ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=5, xonxoff=False, rtscts=False, dsrdtr=False, interCharTimeout=None)

       """ VOLTAGE """
       ser.flushInput()
       ser.flushOutput()
       ser.write('3/1/')
       time.sleep(.1)
       Rail1Volt = ser.readline()
       self.Rail1Voltage.SetValue(Rail1Volt[0:6] + "V")
       Rail2Volt = ser.readline()
       self.Rail2Voltage.SetValue(Rail2Volt[0:6] + "V")
       Rail3Volt = ser.readline()
       self.Rail3Voltage.SetValue(Rail3Volt[0:6] + "V")

       """ CURRENT """
       ser.flushInput()
       ser.flushOutput()
       ser.write('3/2/')
       time.sleep(.1)
       Rail1Cur = ser.readline()
       self.Rail1Current.SetValue(Rail1Cur[0:6] + "A")
       Rail2Cur = ser.readline()
       self.Rail2Current.SetValue(Rail2Cur[0:6] + "A")
       Rail3Cur = ser.readline()
       self.Rail3Current.SetValue(Rail3Cur[0:6] + "A")

       """ POWER """
       Rail1Pow = float(Rail1Cur) * float(Rail1Volt)
       self.Rail1Power.SetValue((str(Rail1Pow))[0:6] + "W")
       Rail2Pow = float(Rail2Cur) * float(Rail2Volt)
       self.Rail2Power.SetValue((str(Rail2Pow))[0:6] + "W") 
       Rail3Pow = float(Rail3Cur) * float(Rail3Volt)
       self.Rail3Power.SetValue((str(Rail3Pow))[0:6] + "W")

       TotalPow = Rail1Pow + Rail2Pow + Rail3Pow
       self.TotalPower.SetValue(str(TotalPow) + "W")

创建如下的GUI:

enter image description here

现在,我面临的问题是让图表实时更新值。为简单起见,我没有尝试将电压,电流和功率读数添加到图表中,而只是执行一个循环来绘制一条线。这是我基于我的程序的代码:

import matplotlib.pyplot as plt
import time
import numpy as np

x=0
y=0 

fig=plt.figure(1)
ax=fig.add_subplot(111)
ax.set_xlim(0,100)
ax.set_ylim(0,1000)
line,=ax.plot(x,y,'-')

for i in range(0,1000):
    x = np.concatenate((line.get_xdata(),[i]))
    y = np.concatenate((line.get_ydata(),[i]))
    line.set_data(x,y)
    plt.pause(.1)
    if i > 100:
        ax.set_xlim(i-100, 100+i)

问题是我不知道在哪里放置此代码/实现它以使其正常工作。如果我使用初始化图的其余部分将其初始化为def,则会执行,但会导致程序等待,直到它将所有点都打开为止。另外,我不知道这对我有什么帮助,因为我想把它放在更新功能中,并在电压/电流/功率测量更新时更新。但是,当我把它放在这个def中时,它会自动加载程序,但不会向图表打印任何数据。

有人能指出我正确的方向吗?提前谢谢。

0 个答案:

没有答案