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

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

Hi David,

···

On 8/6/2014 16:55, David Hughes wrote:

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'.......]

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

Hi Werner,

The StaticBoxSizer (or 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 “”, line 1, in
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.

···

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

Stationmaster


-- Regards
David Hughes
Forestfield Software

Hi Werner,

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
<wxPython API Documentation — wxPython Phoenix 4.2.2 documentation. 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

···

On Wed, Aug 6, 2014 at 12:37 PM, David Hughes <dfh@forestfield.co.uk> wrote:

On 06/08/2014 16:37, Werner wrote:

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

···

2014-08-06 18:37 GMT+02:00 David Hughes <dfh@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

--

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

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 (1.65 KB)

···

Hi David,

  On 8/6/2014 18:37, David Hughes wrote:

Hi Werner,

  The StaticBoxSizer (or 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 “”, line 1, in
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.

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

Stationmaster

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.

···

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.

--
Regards

David Hughes
Forestfield Software

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?

···

On Fri, Aug 8, 2014 at 7:24 AM, David Hughes <dfh@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.

Hi Che,

...

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 (2.11 KB)

···

On 8/8/2014 17:21, C M wrote:

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.

···

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.

--
Regards

David Hughes
Forestfield Software