pythonwxpythonwxformbuilder

Access a specific sizer from wxFormBuilder in Python


I have built a GUI using wxFormBuilder and want to dynamically add buttons to a sizer within the GUI. Here is the wxFormBuilder code.

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

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

import wx
import wx.xrc

###########################################################################
## Class HighVoltage
###########################################################################

class HighVoltage ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 1031,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )

        bSizer84 = wx.BoxSizer( wx.VERTICAL )

        self.m_panel70 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer85 = wx.BoxSizer( wx.HORIZONTAL )

        self.m_staticText79 = wx.StaticText( self.m_panel70, wx.ID_ANY, u"HV Documentation", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText79.Wrap( -1 )
        self.m_staticText79.SetFont( wx.Font( 20, 70, 90, 92, False, "Century Gothic" ) )

        bSizer85.Add( self.m_staticText79, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )


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

        self.m_bitmap24 = wx.StaticBitmap( self.m_panel70, wx.ID_ANY, wx.Bitmap( u"LOGOtransSmall.png", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer85.Add( self.m_bitmap24, 0, wx.ALL, 5 )


        self.m_panel70.SetSizer( bSizer85 )
        self.m_panel70.Layout()
        bSizer85.Fit( self.m_panel70 )
        bSizer84.Add( self.m_panel70, 0, wx.EXPAND |wx.ALL, 5 )

        self.m_panel71 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer86 = wx.BoxSizer( wx.VERTICAL )


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

        self.m_panel73 = wx.Panel( self.m_panel71, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer86.Add( self.m_panel73, 0, wx.EXPAND |wx.ALL, 5 )


        self.m_panel71.SetSizer( bSizer86 )
        self.m_panel71.Layout()
        bSizer86.Fit( self.m_panel71 )
        bSizer84.Add( self.m_panel71, 1, wx.EXPAND |wx.ALL, 5 )

        self.m_panel72 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer87 = wx.BoxSizer( wx.HORIZONTAL )


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

        self.m_button65 = wx.Button( self.m_panel72, wx.ID_ANY, u"Exit", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer87.Add( self.m_button65, 0, wx.ALL, 5 )


        self.m_panel72.SetSizer( bSizer87 )
        self.m_panel72.Layout()
        bSizer87.Fit( self.m_panel72 )
        bSizer84.Add( self.m_panel72, 0, wx.EXPAND |wx.ALL, 5 )


        self.SetSizer( bSizer84 )
        self.Layout()

        self.Centre( wx.BOTH )

    def __del__( self ):
        pass

I want to add buttons to bSizer86 which is a child of m_panel71. Here's what I've tried so far.

class HvDocs(HighVoltage):
    def __init__(self, parent):
        HighVoltage.__init__(self, parent)
        con = sqlite3.connect('hsmanagement.sqlite')
        cur = con.execute('SELECT employee FROM employees WHERE hv=1 ORDER BY employee')
        result = cur.fetchall()
        con.close()
        print result
        sizer = self.GetSizer()
        print type(sizer)
        for f in range(0, len(result) ):
            sizer.Add(wx.Button(self, label=str(result[f][0]), id=int(f), size=(200, 40)), 2, wx.CENTER)

I have tried a few variations of sizer = self.GetSizer() such as sizer = self.m_panel71 and sizer = self.GetSizer(self.m_panel71) but all I can manage is to add the buttons to the top level sizer bSizer84. How do I gain access to bSizer86?


Solution

  • See the following minimal example how to find panels, buttons and sizers by name only. wxFormBuilder offers the possibility to set the wx.Window property window_name for GUI elements you want to keep track on. You should be able to find control you need to know having this name with parent.FindWindowByName(…) without relying on a class attribute.

    import wx
    
    TOK_PNL = 'window_name pnl'
    TOK_BTN = 'window_name btn'
    
    def on_btn(evt, parent):
        btn = parent.FindWindowByName(TOK_BTN)
        pnl = parent.FindWindowByName(TOK_PNL)
    
        newbtn = wx.Button(pnl, -1, 'newbtn')
    
        szsub = btn.GetContainingSizer()
        szsub.Add(newbtn, 1, wx.EXPAND|wx.ALL, 4)
    
        pnl.Layout()
    
    
    class testfrm(wx.Frame):
        def __init__(self, *args, **kwds):
            wx.Frame.__init__(self, *args, **kwds)
            pnl = wx.Panel(self, -1)
            pnl.SetName(TOK_PNL)
    
            btn = wx.Button(pnl, -1, 'Add Button')
            btn.SetName(TOK_BTN)
            szmain = wx.BoxSizer(wx.VERTICAL)
            szsub = wx.BoxSizer(wx.HORIZONTAL)
            szsub.Add(btn, 0, wx.EXPAND|wx.ALL, 4)
            szmain.Add(szsub)
    
            pnl.SetSizer(szmain)
    
            the_parent = self
            btn.Bind(wx.EVT_BUTTON, lambda evt: on_btn(evt, the_parent))
    
    
    if __name__ == '__main__':
        app = wx.App(redirect=False)
        frm = testfrm(None, -1, 'testfrm')
        frm.Show()
        app.MainLoop()