1

我想在 wxPython 的拆分窗口中创建一个实时图(使用 matplotlib?)——到目前为止,我有处理逻辑的代码:我正在创建一个 PID 控制器,我正在寻找添加一个实时图(假设样本之间有 1 秒的延迟,现在使用伪/随机样本)

到目前为止,我的代码在这里(如下)-请告知

import wx
import matplotlib as mpl
mpl.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas

# global constants
Kp = 10
Ki = 2
Kd = 1

# functions used
def AttentionBox( self, message ):
    dialog = wx.MessageDialog(self, message, 'Attention!', wx.ICON_ERROR)
    dialog.ShowModal()
    dialog.Destroy()


def StatusBar( self, message=None):
    statusMessage = "PID constants( " + str(Kp)+","+str(Ki)+","+str(Kd)+" ): "
    self.statusbar.SetStatusText( statusMessage + message )


class PIDFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title="PID Flow Controller", size=(1000,550))
        # split the screen
        self.sp = wx.SplitterWindow(self)
        self.p1 = wx.Panel(self.sp, style = wx.SUNKEN_BORDER)  
        self.p2 = wx.Panel(self.sp, style = wx.SUNKEN_BORDER)
        self.sp.SplitVertically(self.p1, self.p2, 860)
        self.statusbar = self.CreateStatusBar()
        StatusBar(self, "System Idle")

        # define Temperature and Flow Control 
        wx.StaticBox(self.p2, -1, "System Control",             pos=(5, 5), size=(100, 240))
        wx.StaticText(self.p2, -1,"Target (litres/min):",       pos=(15, 100))
        self.Heater     = wx.CheckBox(self.p2, -1 ,  "Heater",  pos=(20,35))
        self.Chiller    = wx.CheckBox(self.p2, -1 ,  "Chiller", pos=(20,65))
        self.TargetFlow = wx.SpinCtrl(self.p2,  -1,  "100",     pos=(15,120), size=(80, 20), min=0, max=350)

        self.ActivateButton = wx.Button(self.p2, -1, "Activate",pos=(20,180))
        self.ActivateButton.Bind(wx.EVT_BUTTON, self.ActivateSystem)

        self.StopButton = wx.Button(self.p2, -1,     "Stop",    pos=(20,215))
        self.StopButton.Bind(wx.EVT_BUTTON, self.StopSystem)


############################################ BUTTONS ######################################################
    def ActivateSystem(self, event):
        if( self.Heater.GetValue() and self.Chiller.GetValue() ):
         StatusBar(self,"Target Flow: " + str(self.TargetFlow.GetValue()) + " litres/min" )
         self.Heater.SetValue(0)
         self.Chiller.SetValue(0)
         AttentionBox( self, "Heater and Chiller? Not Possible - Neither Activated" )   

         elif( self.Heater.GetValue() ):
         StatusBar(self,"Heater Active. Target Flow: " + str(self.TargetFlow.GetValue()) + " litres/min" )

        elif( self.Chiller.GetValue() ):
         StatusBar(self,"Chiller Active. Target Flow: " + str(self.TargetFlow.GetValue()) + " litres/min" )

        else:
         StatusBar(self,"Target Flow: " + str(self.TargetFlow.GetValue()) + " litres/min" )  


    def StopSystem(self, event):
        StatusBar(self, "System Idle" )
        self.Heater.SetValue(0)
        self.Chiller.SetValue(0)    
        self.TargetFlow.SetValue(0)

############################################ GRAPHING ######################################################




app = wx.App(redirect=False)
frame = PIDFrame(None, -1)
frame.Show()
app.MainLoop()
4

0 回答 0