WxPython - choosing the right sizer to use in an application

I find it difficult to understand how to make sizers in wxPython work the way I want them (aside: am I the only one who thinks that wxPython is poorly documented?). I have 4 buttons and textctrl that I want to configure like this:

============================================== |WINDOW TITLE _ [] X| |============================================| |Button1 | Button2 | Button3 | Button4| |--------------------------------------------| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxTextCtrlxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| ============================================== 

Buttons should expand horizontally, but not vertically, and textctrl should expand both horizontally and vertically. I tried almost all the sizer listed in the wxPython demo program and none of them worked - several boxsizers, gridsizers + boxsizeres, a simple gridsizer, flexgridsizer and rowcolsizer, but none of them work, can someone help? For reference, here is the code that I have right now.

  ...snip... panel = wx.Panel(self, -1) select_file = wx.Button(panel, self.BUTTON_0, "Select File") button1 = wx.Button(panel, self.BUTTON_1, "250 Words") button2 = wx.Button(panel, self.BUTTON_2, "500 Words") button3 = wx.Button(panel, self.BUTTON_3, "750 Words") self.txt = wx.TextCtrl(panel, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY) # Now to re-do this with gridsizers instead. # 4 rows, 4 columns, 2 pixel gap horizontally and vertically. grid = rcs.RowColSizer() # These buttons need to expand to fill their space, and then # expand when the windows are resized. grid.Add( select_file, row=1, col=1 ) grid.Add( button1, row=1, col=2 ) grid.Add( button2, row=1, col=3 ) grid.Add( button3, row=1, col=4 ) grid.Add( self.txt, row=2, col=1 ) ...snip... 
+4
source share
2 answers

It took me a while to figure this out. This is what I do with wxPython all the time. Essentially you need to insert boxes. You need one vertical filter block that contains two horizontal blocks.

Here is the code you need:

 import wx class sizertest(wx.Panel): def __init__(self,parent): wx.Panel.__init__(self,parent) vsizer = wx.BoxSizer(wx.VERTICAL) hsizer1 = wx.BoxSizer(wx.HORIZONTAL) button1 = wx.Button(self,-1,"button 1") self.Bind( wx.EVT_BUTTON,self.button1,button1) hsizer1.Add(button1,1,wx.EXPAND) button2 = wx.Button(self,-1,"button 2") self.Bind( wx.EVT_BUTTON,self.button2,button2) hsizer1.Add(button2,1,wx.EXPAND) vsizer.Add(hsizer1,.1,wx.EXPAND) self.SetSizer(vsizer) tc = wx.TextCtrl(self,-1,"",style=wx.TE_MULTILINE) hsizer2 = wx.BoxSizer(wx.HORIZONTAL) hsizer2.Add(tc,1,wx.EXPAND) vsizer.Add(hsizer2,1,wx.EXPAND) def button1(self,event):pass def button2(self,event):pass if __name__ == "__main__": app = wx.App() frame = wx.Frame(parent=None, id=-1, title="sizer test") panel = sizertest(frame) frame.Show() app.MainLoop() 

The vertical sizer vsizer uses two horizontal sizers. The first horizontal sizer contains as many buttons as you want to add to it, and they can expand horizontally. Since you are adding it to a vertical sizer with a number less than one for your "weight", it will not expand vertically. The second horizontal separator holds the text control, and it can expand to take up all the remaining horizontal and vertical space. Both sizers expand as the window expands.

I recommend you get the wxPython book in action by Rappin and Dunn - it really helped me understand how sizer and wxPython screen layouts work.

I use nested boxsizer for 99% of my wxPython programs.

Curt

+8
source

There is a lot of information about the wiki, for example, http://wiki.wxpython.org/UsingSizers . The image below shows how all the flags and arguments of the function work.

Other good examples are -zetcode, and be sure to check out the wxwidgets pages as well.

Samples / demos are also available on the wxpython download page. I cannot provide more than one link because I was a new StackOverflow user.

After a while, playing with sizers, they hopefully become “checkmarks” - I find their use, which makes my GUI easy.

0
source

All Articles