Python: Adding Matplotlib Figure to Auto-Generated Frame Output from wxFormBuilder?

0

Learning Python (coming into it as a strong C# programmer), excited about the power of SciPy and Matplotlib, so I am looking to add a matplotlib figure to a panel. The panel is in a notebook, whose code is generated as the output from wxFormBuilder (see "Main_gui.py" file).

The issue I have is that I am getting an error, and it persists after trying many perturbations of the code:

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


Here's my files:


"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:

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.).


So, what am I doing wrong? Thanks!


Recent edit, thanks to 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()
python
matplotlib
wxpython
wxformbuilder
asked on Stack Overflow Apr 6, 2013 by Nuclid3 • edited Apr 7, 2013 by Nuclid3

1 Answer

0

It looks like in class CreatePlot you are doing calls class calls at the end of it

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

These need to be instance calls to access those methods

I think you might want them to be

parent.page_Data.SetSizer(sizer)
parent.page_Data.Fit()
answered on Stack Overflow Apr 7, 2013 by Yoriz

User contributions licensed under CC BY-SA 3.0