How do you hide a wx.StaticBoxSizer and its contents?

1,347 views
Skip to first unread message

David Hughes

unread,
Aug 6, 2014, 10:55:06 AM8/6/14
to wxpytho...@googlegroups.com
I have got a wx.StaticBoxSizer that contains a list and a couple of buttons. The sizer does its job and defines the group nicely by drawing a box round the group and labels it. I can hide the individual controls inside the sizer but can't hit on the right incantation for hiding the sizer box itself. Is there one or should I put everything on a panel and hide/show that?

>>>self.sbsz
<wx._core.StaticBoxSizer; proxy of <Swig Object of type 'wxStaticBoxSizer *' at 0x70e0460> >
>>>dir(self.sbsz)
['Add', 'AddF', 'AddItem',.... 'Hide',..... 'Show'.......]

-- 
Regards

David Hughes
Forestfield Software

Werner

unread,
Aug 6, 2014, 11:37:45 AM8/6/14
to wxpytho...@googlegroups.com
Hi David,
What about just hiding the wx.StaticBox instance? The sizer can not be
hidden as it is never really shown.

Or do I understand you not correctly?

If you put the StaticBox etc on a Panel, then you should be able to hide
it all by 'just' hiding the panel.

Werner

David Hughes

unread,
Aug 6, 2014, 12:37:36 PM8/6/14
to wxpytho...@googlegroups.com
Hi Werner,


On 06/08/2014 16:37, Werner wrote:
What about just hiding the wx.StaticBox instance?  The sizer can not be hidden as it is never really shown.

Or do I understand you not correctly?

If you put the StaticBox etc on a Panel, then you should be able to hide it all by 'just' hiding the panel.

Werner

The StaticBoxSizer (or Stationmaster as my spell checker want to call it) draws a box around the controls inside it and optionally displays a label,  so I would like to hide these as well otherwise I am left with (in my case) quite a large empty box.

self.stbz.Hide()
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: Hide() takes at least 2 arguments (1 given)

I assume the 1 argument is self, but what else is it expecting?

II was hoping to avoid the hassle of using panels but I'll do that if necessary.

C M

unread,
Aug 6, 2014, 1:09:19 PM8/6/14
to wxpytho...@googlegroups.com
On Wed, Aug 6, 2014 at 12:37 PM, David Hughes <d...@forestfield.co.uk> wrote:
Hi Werner,


On 06/08/2014 16:37, Werner wrote:
What about just hiding the wx.StaticBox instance?  The sizer can not be hidden as it is never really shown.

Or do I understand you not correctly?

If you put the StaticBox etc on a Panel, then you should be able to hide it all by 'just' hiding the panel.

Werner

The StaticBoxSizer (or Stationmaster as my spell checker want to call it) draws a box around the controls inside it and optionally displays a label,  so I would like to hide these as well otherwise I am left with (in my case) quite a large empty box.

self.stbz.Hide()
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: Hide() takes at least 2 arguments (1 given)

I assume the 1 argument is self, but what else is it expecting?

The item for which that sizer is managing the size.  E.g.

self.stbz.Hide(panel5)
self.stbze.Layout()   # or is it Layout() on the parent widget?  I am not sure/haven't tried it.  I'd guess sizer in this case...?

Here's the docs text for sizer.Show(), for which sizer.Hide() is a convenience method of, equal to Show(show=False):

Shows or hides an item managed by the sizer. To make a sizer item disappear or reappear, use Show followed by Layout. The item parameter can be either a window, a sizer, or the zero-based index of the item. Use the recursive parameter to show or hide an item in a subsizer. Returns True if the item was found.

 
II was hoping to avoid the hassle of using panels but I'll do that if necessary.

As I understand it, now for using StaticBox, you should make the StaticBox the parent of the widgets shown within the box (it used to be, prior to wxPython 2.9, you'd make the items shown in it siblings of the staticBox, but not now).  Then, if you call my_static_box.Hide(), it should, I think, hide all the widgets within it... I'd at least try that, if you make sure they are children of the static box.

Che


Vlastimil Brom

unread,
Aug 6, 2014, 1:13:09 PM8/6/14
to wxPython-users
2014-08-06 18:37 GMT+02:00 David Hughes <d...@forestfield.co.uk>:
> ...
> The StaticBoxSizer (or Stationmaster as my spell checker want to call it)
> draws a box around the controls inside it and optionally displays a label,
> so I would like to hide these as well otherwise I am left with (in my case)
> quite a large empty box.
>
> self.stbz.Hide()
> Traceback (most recent call last):
> File "<string>", line 1, in <fragment>
> TypeError: Hide() takes at least 2 arguments (1 given)
>
> I assume the 1 argument is self, but what else is it expecting?
>
> II was hoping to avoid the hassle of using panels but I'll do that if
> necessary.
>
> --
> Regards
>
> David Hughes
> Forestfield Software
>
> --
>
Hi,
i believe, the first argument of Hide(...) is the the item, that
should be hidden; one would call Hide( ) on some higher-order sizer,
possible using recursive=True

Would e.g. the following work for you?
self.Sizer.Hide(self.stbz, recursive=True)
(assumming self is a frame or a panel which has a sizer asigned -
higher than self.stbz, in the same sizer hierarchy).

hth,
vbr

Nathan McCorkle

unread,
Aug 6, 2014, 11:57:48 PM8/6/14
to wxpytho...@googlegroups.com
A sizer is not a wx.Window subclass, so you can't hide or show it. The hide and show methods it has require a wx.Window to be passed, as it's simply a helper function that calls the hide or show of the passed item.

Since the staticbox should be the other widgets' parent, you should be able to hide just that.

http://www.wxpython.org/docs/api/wx.StaticBoxSizer-class.html

http://www.wxpython.org/docs/api/wx.Sizer-class.html#Hide

Werner

unread,
Aug 7, 2014, 4:12:25 AM8/7/14
to wxpytho...@googlegroups.com
Hi David,
It seems also to depend on wxPython version.

If I use Phoenix then both button handlers work for me, if I use 2.8 then only the second one works.  I guess that makes sense as under 2.8 the items in the staticbox are siblings of it and not children.

The Phoenix method of creating a staticbox/sizer should work as of 2.9 but I couldn't make it work in my little test I attach.

Werner
staticboxHide.py

David Hughes

unread,
Aug 8, 2014, 7:24:12 AM8/8/14
to wxpytho...@googlegroups.com

On 07/08/2014 09:12, Werner wrote:
> If I use Phoenix then both button handlers work for me, if I use 2.8
> then only the second one works. I guess that makes sense as under 2.8
> the items in the staticbox are siblings of it and not children.
>
> The Phoenix method of creating a staticbox/sizer should work as of 2.9
> but I couldn't make it work in my little test I attach.

I am using 2.9 and unable to to get it to work. It turns out though that
in the situation where I thought the controls in the box sizer were not
needed there is one case where they are, so I have to keep it visible
anyway.

Thanks to all for your replies.

C M

unread,
Aug 8, 2014, 11:21:53 AM8/8/14
to wxpytho...@googlegroups.com
On Fri, Aug 8, 2014 at 7:24 AM, David Hughes <d...@forestfield.co.uk> wrote:

On 07/08/2014 09:12, Werner wrote:
If I use Phoenix then both button handlers work for me, if I use 2.8 then only the second one works.  I guess that makes sense as under 2.8 the items in the staticbox are siblings of it and not children.

The Phoenix method of creating a staticbox/sizer should work as of 2.9 but I couldn't make it work in my little test I attach.

I am using 2.9 and unable to to get it to work. It turns out though that in the situation where I thought the controls in the box sizer were not needed there is one case where they are, so I have to keep it visible anyway.

Just out of curiosity:  what happened when you called .Hide() on the StaticBox (not the StaticBoxSIZER, but the StaticBox itself) that was the parent of the widgets within it?


Werner

unread,
Aug 9, 2014, 2:47:04 AM8/9/14
to wxpytho...@googlegroups.com
Hi Che,

On 8/8/2014 17:21, C M wrote:
>
>
...
> Just out of curiosity: what happened when you called .Hide() on the
> StaticBox (not the StaticBoxSIZER, but the StaticBox itself) that was
> the parent of the widgets within it?
I don't know what my problem was the other day (got some exception with
2.9), obviously must have done something wrong then.

With the attached code version 2.9 and 3 Phoenix work the same, hiding
the sizer or the staticbox will hide all, in 2.8 only hiding the sizer
will hide it all.

Werner
staticboxHide.py

David Hughes

unread,
Aug 29, 2014, 11:04:17 AM8/29/14
to wxpytho...@googlegroups.com

On 07/08/2014 04:57, Nathan McCorkle wrote:
> A sizer is not a wx.Window subclass, so you can't hide or show it. The hide and show methods it has require a wx.Window to be passed, as it's simply a helper function that calls the hide or show of the passed item.
>
> Since the staticbox should be the other widgets' parent, you should be able to hide just that.

First, apologies for the late response. You are right and my original
understanding was incorrect. I had been using wxDesigner, which only
provides mention of wx.StaticBoxSizer, but looking at the code it
generates, I see:

box = wx.StaticBox( parent, -1, u"Box title" )
sizer = wx.StaticBoxSizer( box, wx.HORIZONTAL)

So, only having a reference to the sizer, I needed to do

sizer.GetStaticBox().Hide()

to achieve the right result.
Reply all
Reply to author
Forward
0 new messages