Python:将Matplotlib图添加到wxFormBuilder的自动生成的帧输出中?

时间:2013-04-06 23:31:29

标签: python matplotlib wxpython wxformbuilder

学习Python(作为一名强大的C#程序员加入),对SciPy和Matplotlib的强大功能感到兴奋,所以我希望在面板上添加一个matplotlib图。该面板位于笔记本中,其代码生成为wxFormBuilder的输出(请参阅“Main_gui.py”文件)。

我遇到的问题是我收到了错误,并且在尝试了许多代码扰动后它仍然存在:

AttributeError: type object 'MainForm' has no attribute 'page_Data'


这是我的文件:


“Main.py”:

import wx

import Main_gui

from numpy import arange, sin, pi

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure

class MainApp( Main_gui.MainForm ):

  # === INITIALIZATION =================================================================================
  def __init__(self, parent):

    #Initialize the main form
    Main_gui.MainForm.__init__( self, parent )

    #Create chart
    self.sizer = self.page_Data.GetSizer()
    self.plot_container = self.page_Data
    self.plotpanel = CreatePlot(self.plot_container)
    self.sizer.Add(self.plotpanel, 1, wx.EXPAND)
    self.plot_container.SetSizer(self.sizer)

class CreatePlot(wx.Panel):

  def __init__(self, parent):

    wx.Panel.__init__(self, parent, -1)

    self.figure = Figure(figsize=(5,4), dpi=100)
    self.axes = self.figure.add_subplot(111)
    t = arange(0.0,3.0,0.01)
    s = sin(2*pi*t)
    self.axes.plot(t,s)

    self.canvas = FigureCanvas(self, -1, self.figure)
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
    Main_gui.MainForm.page_Data.SetSizer(sizer)
    Main_gui.MainForm.page_Data.Fit()

if __name__ == '__main__':

  # === RUN ==========================================================================================
  #Get the wxApp
  app = wx.App(0)
  #Show the form
  MainApp(None).Show()
  #Run
  app.MainLoop()

“Main_gui.py”:

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Oct  8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainForm
###########################################################################

class MainForm ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"MainForm", pos = wx.DefaultPosition, size = wx.Size( 813,512 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        sizer_Main = wx.BoxSizer( wx.VERTICAL )

        self.m_notebook2 = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.NB_BOTTOM|wx.NB_FIXEDWIDTH )
        self.page_Connections = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        flexgrid_Connections = wx.FlexGridSizer( 2, 0, 10, 0 )
        flexgrid_Connections.AddGrowableCol( 0 )
        flexgrid_Connections.AddGrowableRow( 1 )
        flexgrid_Connections.SetFlexibleDirection( wx.BOTH )
        flexgrid_Connections.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )

        sizer1_Connections = wx.StaticBoxSizer( wx.StaticBox( self.page_Connections, wx.ID_ANY, u"Connection" ), wx.VERTICAL )

        sizer1grid_Connections = wx.GridSizer( 0, 3, 0, 0 )

        self.m_staticText3 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"IP Address:", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_LEFT )
        self.m_staticText3.Wrap( -1 )
        sizer1grid_Connections.Add( self.m_staticText3, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 5 )

        self.txt_Connections_IPaddress = wx.TextCtrl( self.page_Connections, wx.ID_ANY, u"10.0.0.3", wx.DefaultPosition, wx.DefaultSize, wx.TE_LEFT )
        self.txt_Connections_IPaddress.SetMaxLength( 15 ) 
        sizer1grid_Connections.Add( self.txt_Connections_IPaddress, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )

        self.btn_Connections_Connect = wx.Button( self.page_Connections, wx.ID_ANY, u"Connect", wx.DefaultPosition, wx.DefaultSize, 0 )
        sizer1grid_Connections.Add( self.btn_Connections_Connect, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )


        sizer1_Connections.Add( sizer1grid_Connections, 0, 0, 5 )


        flexgrid_Connections.Add( sizer1_Connections, 0, wx.EXPAND, 5 )

        sizer2_Connections = wx.StaticBoxSizer( wx.StaticBox( self.page_Connections, wx.ID_ANY, u"Status" ), wx.VERTICAL )

        fgSizer5 = wx.FlexGridSizer( 10, 4, 0, 0 )
        fgSizer5.AddGrowableCol( 2 )
        fgSizer5.SetFlexibleDirection( wx.BOTH )
        fgSizer5.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )

        self.m_staticText5 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Device Name:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText5.Wrap( -1 )
        fgSizer5.Add( self.m_staticText5, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 5 )

        self.txt_Connections_DeviceName = wx.TextCtrl( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        fgSizer5.Add( self.txt_Connections_DeviceName, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )

        self.btn_Connections_RenameDevice = wx.Button( self.page_Connections, wx.ID_ANY, u"Rename", wx.DefaultPosition, wx.DefaultSize, wx.BU_EXACTFIT )
        fgSizer5.Add( self.btn_Connections_RenameDevice, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText6 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"ADC Channels:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText6.Wrap( -1 )
        fgSizer5.Add( self.m_staticText6, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 5 )

        self.txt_Connections_ADCchannels = wx.TextCtrl( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        fgSizer5.Add( self.txt_Connections_ADCchannels, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )

        self.btn_Connections_SetADC = wx.Button( self.page_Connections, wx.ID_ANY, u"Set", wx.DefaultPosition, wx.DefaultSize, wx.BU_EXACTFIT )
        fgSizer5.Add( self.btn_Connections_SetADC, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText15 = wx.StaticText( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText15.Wrap( -1 )
        fgSizer5.Add( self.m_staticText15, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText16 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"HVPS:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText16.Wrap( -1 )
        fgSizer5.Add( self.m_staticText16, 0, wx.ALL, 5 )

        self.m_staticText17 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText17.Wrap( -1 )
        fgSizer5.Add( self.m_staticText17, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText18 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Setpoint:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText18.Wrap( -1 )
        fgSizer5.Add( self.m_staticText18, 0, wx.ALL, 5 )

        self.m_staticText19 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"0.00 V", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText19.Wrap( -1 )
        fgSizer5.Add( self.m_staticText19, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText20 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Stabilized Probe:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText20.Wrap( -1 )
        fgSizer5.Add( self.m_staticText20, 0, wx.ALL, 5 )

        self.m_staticText21 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"False", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText21.Wrap( -1 )
        fgSizer5.Add( self.m_staticText21, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText22 = wx.StaticText( self.page_Connections, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText22.Wrap( -1 )
        fgSizer5.Add( self.m_staticText22, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText7 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Serial Number:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText7.Wrap( -1 )
        fgSizer5.Add( self.m_staticText7, 0, wx.ALL, 5 )

        self.m_staticText8 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"000000", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText8.Wrap( -1 )
        fgSizer5.Add( self.m_staticText8, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText9 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Telnet:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText9.Wrap( -1 )
        fgSizer5.Add( self.m_staticText9, 0, wx.ALL, 5 )

        self.m_staticText10 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText10.Wrap( -1 )
        fgSizer5.Add( self.m_staticText10, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )

        self.m_staticText11 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"TFTP:", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText11.Wrap( -1 )
        fgSizer5.Add( self.m_staticText11, 0, wx.ALL, 5 )

        self.m_staticText12 = wx.StaticText( self.page_Connections, wx.ID_ANY, u"Disabled", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText12.Wrap( -1 )
        fgSizer5.Add( self.m_staticText12, 0, wx.ALL, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        fgSizer5.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )


        sizer2_Connections.Add( fgSizer5, 1, wx.EXPAND, 5 )


        flexgrid_Connections.Add( sizer2_Connections, 0, wx.EXPAND, 5 )


        self.page_Connections.SetSizer( flexgrid_Connections )
        self.page_Connections.Layout()
        flexgrid_Connections.Fit( self.page_Connections )
        self.m_notebook2.AddPage( self.page_Connections, u"Connections", False )
        self.page_Acquisition = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Acquisition, u"Acquisition", False )
        self.page_Data = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer3 = wx.BoxSizer( wx.VERTICAL )


        self.page_Data.SetSizer( bSizer3 )
        self.page_Data.Layout()
        bSizer3.Fit( self.page_Data )
        self.m_notebook2.AddPage( self.page_Data, u"Data", True )
        self.page_Calibration = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Calibration, u"Calibration", False )
        self.page_Analyses = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Analyses, u"Analyses", False )
        self.page_Locator = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Locator, u"Locator", False )
        self.page_Hardware = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Hardware, u"Hardware", False )
        self.page_Log = wx.Panel( self.m_notebook2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.m_notebook2.AddPage( self.page_Log, u"Log", False )

        sizer_Main.Add( self.m_notebook2, 1, wx.BOTTOM|wx.EXPAND|wx.TOP, 5 )


        self.SetSizer( sizer_Main )
        self.Layout()

        self.Centre( wx.BOTH )

    def __del__( self ):
        pass

回溯:

Traceback (most recent call last):
  File "<project location>\Main.py", line 56, in <module>
    MainApp(None).Show()
  File "<project location>\Main.py", line 27, in __init__
    self.plotpanel = CreatePlot(self.plot_container)
  File "<project location>\Main.py", line 47, in __init__
    Main_gui.MainForm.page_Data.SetSizer(sizer)
AttributeError: type object 'MainForm' has no attribute 'page_Data'
19:12:12: Debug: ..\..\include\wx/msw/private.h(697): 'UnregisterClass' failed with    error 0x00000584 (class still has open windows.).


那么,我做错了什么?谢谢!


最近编辑,感谢Yoriz:

class MainApp( Main_gui.MainForm ):

  # === INITIALIZATION =================================================================================
  def __init__(self, parent):

    #Initialize the main form
    Main_gui.MainForm.__init__( self, parent )

    #Create chart
    self.sizer = self.page_Data.GetSizer()
    self.plot_container = self.page_Data
    self.plotpanel = CreatePlot(self.plot_container)

class CreatePlot(wx.Panel):

  def __init__(self, parent):

    wx.Panel.__init__(self, parent, -1)

    self.figure = Figure(figsize=(5,4), dpi=100)
    self.axes = self.figure.add_subplot(111)
    t = arange(0.0,3.0,0.01)
    s = sin(2*pi*t)
    self.axes.plot(t,s)

    self.canvas = FigureCanvas(parent, -1, self.figure)
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
    parent.SetSizer(sizer)
    parent.Fit()

1 个答案:

答案 0 :(得分:0)

在类CreatePlot中看起来就是在它的末尾进行调用类调用

Main_gui.MainForm.page_Data.SetSizer(sizer)
Main_gui.MainForm.page_Data.Fit()

这些需要是访问这些方法的实例调用

我想你可能希望他们成为

parent.page_Data.SetSizer(sizer)
parent.page_Data.Fit()