[wxPython-users] newbie and sizers

Christopher Barker Chris.Barker at noaa.gov
Wed Dec 20 12:26:06 PST 2006


Roggie Boone wrote:
> First time poster to this list.

A few suggestions:

1) post your code as an enclosure, rather than inline, so it doesn't get 
mangled by mail software.

2) post a complete, working application that demonstrates your problem 
so that we can test and tweak it. It's easier if it's in one file.

3) take a look at:

http://wiki.wxpython.org/index.cgi/wxPython_Style_Guide

for style suggestions


> I know it's a newbie mistake, but I'm having
> trouble understanding sizers.

It does take a while to wrap your brain around them.

I thought
> using a VERTICAL sizer would put them under
> each other.

you've got the idea right, but a fair bit of odd code. I"m not going to 
try to run your code, but I'll sprinkle suggestions in:

> please ignore newbie coding practices.

Rather than ignore -- I'll offer alternatives.

> import wx
> from wxPython.wx import *

import wx will do it -- you'll have an odd mixture this way. And DON'T 
"import *" ever.

>          panel = OverviewPanel(self,width,height)

you don't need to give it a size, that's why you use sizers!

>          ws = panel.GetClientSize()
>          nb =
> MainNoteBook(self,ws.x,ws.y,width,height)

same here, just do:
      nb = MainNoteBook(self)

the sizing and positioning will be taken care of.

>          self.Sizer.Add(panel,0)
>          self.Sizer.Add(nb,1)
you need a call something like this:

           self.SetSizerAndFit(self.Sizer)

> class OverviewPanel(wx.Panel):
>      def __init__(self, parent, width, height):
>          wx.Panel.__init__(self, parent, -1, style=wx.RAISED_BORDER)

don't pass in width and height, so this can be:
     def __init__(self, parent):
           wx.Panel.__init__(self, style=wx.RAISED_BORDER)

everything else will get a default.

Alternatively, I like to do:

     def __init__(self, *args, **kwargs):
           wx.Panel.__init__(self, *args, **kwargs)

That way, all the arguments will get passed through to the wx.Panel 
init. You can specify the style when you create your instance.


>          self.Sizer = wxBoxSizer(wxVERTICAL)

>          PCTWIDTH = 0.20
>          PCTHEIGHT = 1.0
>          
>          ws = parent.GetClientSize()
>          self.SetSize((int(ws.x*PCTWIDTH),
> int(ws.y*PCTHEIGHT))) 

you don't need this -- let the Sizers do it.

>          dateLabel = wx.StaticText(self,-1,"Date:")

I like the style:
	datelabel = wx.StaticText(self, label="Date:")

rather than passing on -1 all over the place -- keyword arguments are 
wonderful!	

>          self.Sizer.Add(dateLabel,0)
>          self.Sizer.Add(button,0)
>          self.Sizer.Add(button2,0)
>          
>          self.SetAutoLayout(true)

I think you need a SetSizer Call here:
           self.SetSizerAndFit(self.Sizer)


Once you've got that working, you'll probably need to put some space in.
> class MainNoteBook(wx.Window):
>     def __init__(self,parent,x,y,width,height):
>           wx.Window.__init__(self,parent, -1, (x+3,0),
> (0,0), 0, "MainNotebook" )

same as above, let the Sizers do the job!

>           nb.SetSize((int(ws.x*PCTWIDTH),
> int(ws.y*PCTHEIGHT))) 
>           self.SetSize((int(ws.x*PCTWIDTH),
> int(ws.y*PCTHEIGHT)))    

ditto!

I think the missing SetSizer calls are you primary problem.

-Chris



-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list