[wxPython-users] plese suggest good tutorial for sizers.
Christopher Barker
Chris.Barker at noaa.gov
Thu Jan 4 13:13:04 PST 2007
krishnakant Mane wrote:
> can some one suggest a good tutorial to learn sizers in details?
There are a few in the Wiki -- do a title search for "Sizer". In
particular, there is:
http://wiki.wxpython.org/index.cgi/UsingSizers
http://wiki.wxpython.org/index.cgi/ChallengeSizers
> I am particularly interested to know that how I can put grid sizers
> and box sizers to best use and also get control over the actual size
> of controls.
for the most part, you don't control the actual size of the controls --
that's the whole point of sizers. However, if you need to, you can fix
the size of a given control ( I think SetSizeHints() does it)
> some times grids make the controls of same size and I don't want it.
A GridSizer will make all the controls the same size. A FlexGridSizer
will make each column and each row the size it needs to be.
> at the same time I may like to increase or decrease the sizes of the
> gapps between the controls.
When you Add() a control to a Sizer, you can put a space around it on
the side that want -- specify the side with flags: wx.TOP, wx.RIGHT, etc
-- or wx.ALL for all. Then the size of that space is specified in pixels
by the next parameter.
With a GridSizer or FlexGridSizer, you can specify the space between the
rows and columns. You can also put in spacers in any location to
customize the spacing.
> ok but I will like to leave some wide gap between my 4 rows with edit
> fields and the line of buttons, will I be able to do that?
yes, you can put a spacer in anywhere -- either at a fixed size, or make
it stretchable.
> and if the box sizer is vertical, how can I put a custom grid of
> lebel-text pares?
Key to this is that sizers can be (and often have to be) nested. In your
case, the grid of label-text pairs is one sizer, the buttons are
another, and those both get put in the vertical box sizer.
> the layout will be not actually 2 colums but 4 columns. a lable-text
> pare with very little space between the static text and the textctrl
> then some significant gap before the other pare. means column 1,2
> with little or no space and then some significant space and then
> column 3 and 4.
the way to do that is to add an extra column for a spacer. so in your
case, it would look something like (untested):
fgs = wx.FlexGridSizer(nrows=3, ncols=5, hgap=4, vgap=4)
# 1st label-text pair
fgs.Add(label1, 0, wx.ALIGN_RIGHT)
fgs.Add(text1, 1, wx.ALIGN_LEFT)
# a space
fgs.Add((10,1), 0)
# 2nd label-text pair
fgs.Add(label2, 0, wx.ALIGN_RIGHT)
fgs.Add(text2, 1, wx.ALIGN_LEFT)
## now a new row will be started
# 3rd label-text pair
fgs.Add(label3, 0, wx.ALIGN_RIGHT)
fgs.Add(text3, 1, wx.ALIGN_LEFT)
# a space
fgs.Add((10,1), 0)
# 4th label-text pair
fgs.Add(label4, 0, wx.ALIGN_RIGHT)
fgs.Add(text4, 1, wx.ALIGN_LEFT)
etc....
You can see how this might be done programatically in a loop, rather
than writing all this redundant code.
By the way, As it is often the case that you want to use a label and a
text box as essentially one control, I wrote a little module that helps
with that. I've enclosed it.
> ok got the point but will I be able to change the sizes of the buttons
> once I tell the box to lay them on the screen?
If you need to change the size of a control, you can do so, then call
Layout() on the parent sizer -- you generally don't want to do that,
though, unless you change what is on or in the control.
> can a spacer be altered for different sizes and how?
A spacer can be any (width, height) size, and it can be put in the sizer
to be stretchable or not -- its often useful to put a small stretchable
space in between two controls to allow the controls to stay aligned to
the right and left as the Window grows.
> I want to know if my code will become to complex if there are two many
> controls and if yes how to avoid it?
It can get pretty complex, but in general, when you have a lot of
controls, they are laid out in logical groups -- essentially each group
gets a sizer. In addition, if you really have a lot, they are probably
grouped in functional groups -- and should be put on a panel in a
separate class -- then these custom Panels can be laid out together.
Keep in mind that "easy to learn" and "easy to use" are not the same. I
think sizers are easy to use, in the sense that they will make you a
more productive programmer, but they are not easy to learn -- they do
take a while to wrap your brain around.
-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