confused with spacers in box layout.

Saturday, May 5, 2007, 3:49:47 PM, krishnakant Mane wrote:

hello,
I have a couple of questions on box layout.
firstly, is it possible to create a complete frame with two rows of
lable-text box pares, well, actually 4 rows with a big spacer in
between to seperate one lable-editctrl pare?
and every row (horisontal box) should be having a gap before the
other row starts. I mean to say that after the first row of
lable-textbox pares is over there should be a gap of say 10 pikcells
and then the other row should start.
I want equal marjins at the left and right side of the frame.
and before the row of buttons starts, I want a big gap between the
last row of textboxes and the row of buttons may be a vertical gap of
50 pics.
is that possible with the use of just horizontal and verticle boxes?
regards,

Yes, it is possible to use only horizontal and verticle boxes to do
that, but I wouldn't wish that approach upon my worst enemy. :wink:

Please see the attached code: it uses only one GridBagSizer for
everything. Well, almost: the row of buttons seemed easier to put
inside a separate horizontal sizer. I used spacers to guarantee that
both margins are the same, and borders to render the gaps between
rows.

-- tacao

No bits were harmed during the making of this e-mail.

sizer_test.py (1.53 KB)

Yes, it is possible to use only horizontal and verticle boxes to do
that, but I wouldn't wish that approach upon my worst enemy. :wink:

and why do you feel so? I have nither used box layouts and sizers nor
gridbag layout. can you give me any reasons why I should not be using
box layouts both horizontal and vertical for my form with row of
textboxes and one row of buttons in the bottom?
is there some thing which this approach can't achieve which gridbag can do?

Please see the attached code: it uses only one GridBagSizer for
everything. Well, almost: the row of buttons seemed easier to put
inside a separate horizontal sizer. I used spacers to guarantee that
both margins are the same, and borders to render the gaps between
rows.

yes I noticed that you added a horizontal box sizer for buttons. can
I ask the reason? again this is due to lak of my experience. Please
guide me.
regards,
Krishnakant.

···

On 06/05/07, E. A. Tacao <e.a.tacao@estadao.com.br> wrote:

Box sizers work very well for simple layouts, like a row of buttons,
_and_ for complex layouts. The problem I see is that when designing a
GUI from ground up only with box sizers, chances are that the GUI will
become complex and one will end putting sizers inside sizers inside
sizers inside... It may work eventually, but debugging a code with
lots of deep nested sizers in search of a misaligned widget, for
example, is an awful task that may take a long time.

yes Tacao, I noticed this after reading your code. although I did not
understand the parameters you passed to the spacer. I understood the
row and comumn but what was before that in the nested ()? was that
the left and top size of the spacer or top and left? you see, I am a
blind person and can't make a guess, so want to know. actually I am
also doing my research on how blind people can create good layouts and
I think the gridbag was pritty good enough.
second thing I wanted to understand is that when u added the
horizontal box, I think you have given the span option, can you
explain that? is it to say that this particular box will occupy the
entire row? does it start from 0 or 1?
I also want to know what is basic difference between a flexgrid and
gridbag layout.

Also, a GridBagSizer allows for explicit positioning and spanning. I
find a lot easier to simply 'tell' wx to put something at row 2, col
1, instead of handling the inserts and appends the box sizers require.

that's really good, but can I also set the minimum and maximumn sizes
of controls like I can do in box layouts? if yes then how?

regards,
Krishnakant.

···

On 06/05/07, E. A. Tacao <e.a.tacao@estadao.com.br> wrote:

Sunday, May 6, 2007, 3:21:16 AM, krishnakant Mane wrote:

although I did not understand the parameters you passed to the
spacer. I understood the row and comumn but what was before that in
the nested ()? was that the left and top size of the spacer or top
and left? you see, I am a blind person and can't make a guess, so
want to know. actually I am also doing my research on how blind
people can create good layouts and I think the gridbag was pritty
good enough.

The line

  sizer.AddSpacer((50, 10), pos=(row, 0))

is adding a 50x10 (50 pixels x 10 pixels) empty space to the row 'row'
and column 0 of the sizer.

and the line

  sizer.Add(st, pos=(row, 1), border=20,
            flag=wx.TOP|wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_LEFT)

is adding 'st' (the StaticText) to the row 'row' and column 1 of the
sizer. The StaticText will be aligned to the left and will be centered
vertically in the grid. That grid cell will also have a 20 pixel
height border on the top, i. e., a 20 px empty space separating the
StaticText and the top of the grid cell.

second thing I wanted to understand is that when u added the
horizontal box, I think you have given the span option, can you
explain that? is it to say that this particular box will occupy the
entire row?

Yes.

does it start from 0 or 1?

Positions always start from 0.

Spans always start from 1. So that a 'span=(1,1)' is redundant, and
'span=(0,x)' or 'span=(x,0)' are errors.

A GridBagSizer showing the 'pos' (position) parameters; no cell was
spanned:

···

+-------+-------+-------+

(0,0) | (0,1) | (0,2) |

+-------+-------+-------+

(1,0) | (1,1) | (1,2) |

+-------+-------+-------+

(2,0) | (2,1) | (2,2) |

+-------+-------+-------+

A GridBagSizer where the (1,0) position was spanned to occupy 1 row
and 3 columns:

+-------+-------+-------+

(0,0) | (0,1) | (0,2) |

+-------+-------+-------+

  (1,0), span=(1,3) |

+-------+-------+-------+

(2,0) | (2,1) | (2,2) |

+-------+-------+-------+

Another GridBagSizer where the (1,1) position was spanned to occupy 2
rows and 2 columns:

+-------+-------+-------+-------+

      > > > >

+-------+-------+-------+-------+

      > (1,1), | |

+-------+ span=(2,2) +-------+

      > > >

+-------+-------+-------+-------+

      > > > >

+-------+-------+-------+-------+

I also want to know what is basic difference between a flexgrid and
gridbag layout.

I strongly recommend you to that a look at the docs, since they
explains that better than me. :wink: Quoting the docs:

"""
A FlexGridSizer is a sizer which lays out its children in a
two-dimensional table with all table fields in one row having the same
height and all fields in one column having the same width, but all
rows or all columns are not necessarily the same height or width as in
the wxGridSizer.

GridBagSizer is a wxSizer that can lay out items in a virtual grid
like a wxFlexGridSizer but in this case explicit positioning of the
items is allowed using wxGBPosition, and items can optionally span
more than one row and/or column using wxGBSpan.
"""

Also, I strongly recommend you to donwload and install the wxPython
Demo. It has several examples on how to use everything in wxPython,
including sizers:
http://www.wxpython.org/download.php

The wiki is also your friend. There are a lot of helpful pages
regarding sizers, for example:
http://wiki.wxpython.org/index.cgi/UsingSizers
http://wiki.wxpython.org/index.cgi/wxSizer_in_python
http://wiki.wxpython.org/index.cgi/wxGridBagSizer

can I also set the minimum and maximumn sizes of controls like I can
do in box layouts? if yes then how?

Yes. You do that in the control itself, via SetMinSize and
SetMaxSize methods.

-- tacao

No bits were harmed during the making of this e-mail.

Sunday, May 6, 2007, 3:21:16 AM, krishnakant Mane wrote:

The line

  sizer.AddSpacer((50, 10), pos=(row, 0))

is adding a 50x10 (50 pixels x 10 pixels) empty space to the row 'row'
and column 0 of the sizer.

ok that's clear, so that will also add a gap between rows is it?

and the line

  sizer.Add(st, pos=(row, 1), border=20,
            flag=wx.TOP|wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_LEFT)

is adding 'st' (the StaticText) to the row 'row' and column 1 of the
sizer. The StaticText will be aligned to the left and will be centered
vertically in the grid. That grid cell will also have a 20 pixel
height border on the top, i. e., a 20 px empty space separating the
StaticText and the top of the grid cell.

so will this not cause a alignment problem? the spacer had different
height than the border of the static text. what will be the size of
the cells in such a situation? I am confused because the cell that
contains the spacer has a height of 10 and the border will stretch the
cell for the static text upto 20.

> does it start from 0 or 1?

Positions always start from 0.

Spans always start from 1. So that a 'span=(1,1)' is redundant, and
'span=(0,x)' or 'span=(x,0)' are errors.

does that mean that if I want to position a horizontal box on the 0th
column in the 50th row and want to span all the columns and asuming
there are 5 columns,
I will give pose as 0,50 and span as 1,5. is that right?

regards,
Krishnakant.

···

On 06/05/07, E. A. Tacao <e.a.tacao@estadao.com.br> wrote:

hi,
sorry to post again.
I just forgot one point.
I wont be having default values in my TextCtrl and so need to set the
size of the textboxes.
can I do that to the TextCtrl before placing them in the positions?
I am slowly getting the idea of positioning and spanning.
it seams to me that span should only be used if one needs to increase
the area of a control without disturbing the settings of other cells
is that right?
I understand that if I want to have a control bigger in size without
affecting all other cells then I should not set sizers but span the
control over cells. This is my understanding but may be wrong.
regards.
Krishnakant.

Sunday, May 6, 2007, 12:15:41 PM, krishnakant Mane wrote:

Sunday, May 6, 2007, 3:21:16 AM, krishnakant Mane wrote:

The line

  sizer.AddSpacer((50, 10), pos=(row, 0))

is adding a 50x10 (50 pixels x 10 pixels) empty space to the row
'row' and column 0 of the sizer.

ok that's clear, so that will also add a gap between rows is it?

It depends on what you're calling 'gap'. That line adds a spacer with
10px height, so if the widgets you have in the same row 'row' happen
to have all of them less than 10px of height, then yes, you'll notice
a gap between rows in this case. IOW, the sizer will set the row
height so that it has at least the height of the bigger widget on that
row.

and the line

  sizer.Add(st, pos=(row, 1), border=20,
            flag=wx.TOP|wx.ALIGN_CENTRE_VERTICAL|wx.ALIGN_LEFT)

is adding 'st' (the StaticText) to the row 'row' and column 1 of the
sizer. The StaticText will be aligned to the left and will be centered
vertically in the grid. That grid cell will also have a 20 pixel
height border on the top, i. e., a 20 px empty space separating the
StaticText and the top of the grid cell.

so will this not cause a alignment problem? the spacer had different
height than the border of the static text. what will be the size of
the cells in such a situation? I am confused because the cell that
contains the spacer has a height of 10 and the border will stretch
the cell for the static text upto 20.

The cell that that contains the spacer has NOT a height of 10. You
never specify the widths nor the heights of sizers cells. The purpose
of a sizer, after all, is to calculate that itself.

The cell in question only was told to receive a spacer with a height
of 10. The height of the cell will eventually depend on what height
the row will need to receive all the widgets on a given row.

So, since the height of the row will be at least the height of the
taller widget on that row, yes, if you add controls (or spacers) with
different sizes to a row they could become misaligned, and that's what
the flags wx.ALIGN_CENTRE_HORIZONTAL, wx.ALIGN_BOTTOM, etc., are for:
to specify how to align the controls if controls of different sizes
are being added to a sizer.

> does it start from 0 or 1?

Positions always start from 0.

Spans always start from 1. So that a 'span=(1,1)' is redundant, and
'span=(0,x)' or 'span=(x,0)' are errors.

does that mean that if I want to position a horizontal box on the 0th
column in the 50th row and want to span all the columns and asuming
there are 5 columns,
I will give pose as 0,50 and span as 1,5. is that right?

Almost. The 'pos' keyword requires a tuple of 2 values, where the
first is the row, the second is the column. Rows also come first in
the span keyword tuple. In that case it would be
pos=(50, 0), span=(1, 5).

-- tacao

No bits were harmed during the making of this e-mail.

···

On 06/05/07, E. A. Tacao <e.a.tacao@estadao.com.br> wrote:

Sunday, May 6, 2007, 12:33:59 PM, krishnakant Mane wrote:

I wont be having default values in my TextCtrl and so need to set
the size of the textboxes.

Controls inherently have a size, regardless if they have been set
a value or not. You only need to change it if you need a control
bigger/smaller than what wxPython assumes as being the controls's
default size.

can I do that to the TextCtrl before placing them in the positions?

Yes.

I am slowly getting the idea of positioning and spanning.
it seams to me that span should only be used if one needs to increase
the area of a control without disturbing the settings of other cells
is that right?

Yes.

I understand that if I want to have a control bigger in size without
affecting all other cells then I should not set sizers but span the
control over cells. This is my understanding but may be wrong.

Yes.

-- tacao

No bits were harmed during the making of this e-mail.

Sunday, May 6, 2007, 12:46:36 PM, E. A. Tacao wrote:

Sunday, May 6, 2007, 12:33:59 PM, krishnakant Mane wrote:

I understand that if I want to have a control bigger in size without
affecting all other cells then I should not set sizers but span the
control over cells. This is my understanding but may be wrong.

Yes.

Er... I mean "yes, you span the control over cells". :slight_smile:

-- tacao

No bits were harmed during the making of this e-mail.