How to remove a size(spacer) from a sizer ?

596 views
Skip to first unread message

ErwinP

unread,
Mar 28, 2013, 11:22:58 AM3/28/13
to wxpytho...@googlegroups.com
Hello,

I have added a size (not a sizer) to a sizer:
sizer = wx.BoxSizer(wx.VERTICAL) 
spacer = wx.Size(100,100)
sizer.Add(spacer, 0, wx.EXPAND)
When I try to remove the size,
sizer.Detach(spacer)
I get this exception:
TypeError: wx.Window, wx.Sizer or int (position) expected for item

How can I remove a size from a spacer ?

Boštjan Mejak

unread,
Mar 28, 2013, 12:23:55 PM3/28/13
to wxpytho...@googlegroups.com
Instead of using wx.Size(), you can use wx.AddSpacer(). ;)

Cody

unread,
Mar 28, 2013, 12:43:08 PM3/28/13
to wxpytho...@googlegroups.com
Hi,

For spacers it oddly seems the only way is to pass the explicit position (index) of the SizerItem that contains the spacer. Seems like Detach and other methods should also work on SizerItems.

sizer.Detach(0) # its the first item in the sizer in this example

On top of this it appears that the index also needs to be manually tracked as the sizers appear to make copies of the SizerItems so code like the following _will_ _not_ work when trying to locate the items index in the sizer.

spacer = sizer.Add(spacer, 0, wx.EXPAND)
for idx, item in enumerate(sizer.Children):
    if item is spacer:
        sizer.Detach(idx)
        break


Cody


Robin Dunn

unread,
Mar 28, 2013, 12:49:28 PM3/28/13
to wxpytho...@googlegroups.com
$ pydoc wx.Sizer.Detach
Help on method Detach in wx.Sizer:

wx.Sizer.Detach = Detach(*args, **kwargs) unbound wx._core.Sizer method
Detach(self, item) -> bool

Detaches an item from the sizer without destroying it. This method
does not cause any layout or resizing to take place, call `Layout` to
do so. The *item* parameter can be either a window, a sizer, or the
zero-based index of the item to be detached. Returns True if the
child item
was found and detached.


To detach a spacer you need to use the zero-based index of the item in
the sizer. In other words, in the code above you would use
sizer.Detach(0) because the spacer was the first item added to the sizer.


>
> --
> You received this message because you are subscribed to the Google
> Groups "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to wxpython-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
Robin Dunn
Software Craftsman
http://wxPython.org

ErwinP

unread,
Mar 28, 2013, 3:54:18 PM3/28/13
to wxpytho...@googlegroups.com
Hello,

I do not know exactly how to determine the index of an item in a sizer. 
I guess a BoxSizer appends each added item to the end of a list and when an item is removed, the index of the items behind are decremented.
But what about 2-dimensional sizers (e.g.GridBagSizer) where items are added with a pos=(row,col) parameter. Does the index depend on the table-position (e.g. pos(0,0)=index 0, pos(0,1)=index 1, ...) or does it correlate to the chronological order of the add-calls ?

Robin Dunn

unread,
Mar 30, 2013, 12:24:48 AM3/30/13
to wxpytho...@googlegroups.com
I think it is still the order of the add-calls.

If you don't need to put something else in that grid sizer cell then
instead of detaching it you could just do something like saving the
sizer item that was returned when adding it, and change the size (using
SetSpacer) to 0,0 and then call Layout. That will be equivalent to
removing the spacer from the sizer.
Reply all
Reply to author
Forward
0 new messages