user interface - wxPython: access sizer of GUI generated by wxFormBuilder and replace child -


i want buid applications under idea of mvc, sepparate gui , controller. in addition, have workmate better me in graphical tasks, , want distribute work: build graphical part wxformbuilder , build "machinery" of application.

when have gui generated, want substitute elements wxformbuilder cannot manage: example, wxobjectlistview. idea create gui normal wxlistbox, import main program, , substitute wxobjectlistview. don't want modify directly generated code wxformbuilder, because want maintain backwards compatibility gui editor.

the problem , question are: main program imports gui, how can access sizer contains list, delete , substitute objectlistview? sizer.delete(list) , sizer.add(olv)...

here example:

the gui code generated wxformbuilder: frame list , button.

# -*- coding: utf-8 -*-   ########################################################################### ## python code generated wxformbuilder (version sep  8 2010) ## http://www.wxformbuilder.org/ ## ## please "not" edit file! ###########################################################################  import wx  ########################################################################### ## class myframe1 ###########################################################################  class myframe1 ( wx.frame ):      def __init__( self, parent ):         wx.frame.__init__ ( self, parent, id = wx.id_any, title = wx.emptystring, pos = wx.defaultposition, size = wx.size( 330,288 ), style = wx.default_frame_style|wx.tab_traversal )          self.setsizehintssz( wx.defaultsize, wx.defaultsize )          fgsizer = wx.flexgridsizer( 2, 1, 0, 0 )         fgsizer.addgrowablecol( 0 )         fgsizer.addgrowablerow( 0 )         fgsizer.setflexibledirection( wx.both )         fgsizer.setnonflexiblegrowmode( wx.flex_growmode_specified )          m_listbox1choices = [ u"row1", u"row2" ]         self.m_listbox1 = wx.listbox( self, wx.id_any, wx.defaultposition, wx.defaultsize, m_listbox1choices, 0 )         fgsizer.add( self.m_listbox1, 1, wx.all|wx.expand, 5 )          self.m_button1 = wx.button( self, wx.id_any, u"mybutton", wx.defaultposition, wx.defaultsize, 0 )         fgsizer.add( self.m_button1, 1, wx.all|wx.expand, 5 )          self.setsizer( fgsizer )         self.layout()          self.centre( wx.both )      def __del__( self ):         pass 

and here main code, imports graphical class.

from __future__ import print_function # -*- coding: utf-8 -*- import wx olvgui import myframe1  class ventana(myframe1):      def inicia(self):         hijos = self.getchildren()         h in hijos:             print(h)   if __name__ == "__main__":     app = wx.app(false)     v = ventana(none)     v.inicia()     v.show()     app.mainloop() 

when attempt access frame children, see "final" objects, not sizer.

<wx._controls.listbox; proxy of <swig object of type 'wxlistbox *' @ 0x1e0cea8> > <wx._controls.button; proxy of <swig object of type 'wxbutton *' @ 0x19a5f90> > 

maybe gui must have structure if want make elements accesibles, have no idea how make it. have tried make 2 levels of sizers, , final result same: can access final elements, not sizers.

you need change fgsizer self.fgsizer can access later. can use sizer's remove method remove widget. there insert method can use insert widget in it's place.

see documentation on sizers full details:

because removing , inserting widget, need call layout @ end of method. article has example:

finally, here's example of getting sizer:

import wx  ######################################################################## class myframe(wx.frame):     """"""      #----------------------------------------------------------------------     def __init__(self):         """constructor"""         wx.frame.__init__(self, none, title='test')         self.panel = wx.panel(self)          sizer = wx.boxsizer(wx.vertical)          btn = wx.button(self.panel, label="get sizer")         btn.bind(wx.evt_button, self.ongetsizer)         sizer.add(btn, 0, wx.all|wx.center, 5)         self.panel.setsizer(sizer)          self.show()      #----------------------------------------------------------------------     def ongetsizer(self, event):         """"""         sizer = self.panel.getsizer()  if __name__ == '__main__':     app = wx.app(false)     frame = myframe()     app.mainloop() 

here example of saving widget's index:

self.widget_dict = {} some_boxsizer.add(some_widget)  # first widget, id 0 self.widget_dict[some_widget] = 0 

now in event handler, can use dictionary determine widget in index:

index = self.widget_dict[some_widget] 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -