Resizing dialog based on panel size

I have a dialog that contains a panel which can change size. I need
the dialog to resize itself when the panel changes size (growing to
show previously-hidden settings). However, I'm getting very strange
behavior where the height of the panel gets set to a clearly-invalid
value (32767 or 0). Of course this causes the dialog to bug out.

I put together a simple test app here: http://pastebin.com/ntfFvcfV

Click the button in the dialog, and the dialog's height will explode
to 32767. If the StaticText is removed, then it will instead collapse
to a height of 0.

Any ideas what's going on here?

-Chris

You had this code:

         self.panelSizer.Add(self.button, wx.ALIGN_CENTER | wx.TOP, 5)

Notice that the second parameter is the flags and that you have left out the proportion value. So the sizer is instead using wx.ALIGN_CENTER|wx.TOP as the proportion and 5 as the flags.

Also, I would advise to avoid using SetSizerAndFit, because contrary to its name it is not the same as SetSizer followed by Fit. Instead just use Fit on the top level window and then the resizing that is done for that will trigger the layout of the windows contained within it.

It's also not necessary to reset the sizers in the button handler. Just call the dialog's Fit again.

resize.py (1.46 KB)

···

On 10/19/12 2:12 PM, Chris Weisiger wrote:

I have a dialog that contains a panel which can change size. I need
the dialog to resize itself when the panel changes size (growing to
show previously-hidden settings). However, I'm getting very strange
behavior where the height of the panel gets set to a clearly-invalid
value (32767 or 0). Of course this causes the dialog to bug out.

I put together a simple test app here: import wxclass Dialog(wx.Dialog): def __init__(self, parent): - Pastebin.com

Click the button in the dialog, and the dialog's height will explode
to 32767. If the StaticText is removed, then it will instead collapse
to a height of 0.

Any ideas what's going on here?

--
Robin Dunn
Software Craftsman

You had this code:

        self.panelSizer.Add(self.button, wx.ALIGN_CENTER | wx.TOP, 5)

Notice that the second parameter is the flags and that you have left out the
proportion value. So the sizer is instead using wx.ALIGN_CENTER|wx.TOP as
the proportion and 5 as the flags.

D'oh! Stupid mistake. Thanks for the correction.

Also, I would advise to avoid using SetSizerAndFit, because contrary to its
name it is not the same as SetSizer followed by Fit. Instead just use Fit
on the top level window and then the resizing that is done for that will
trigger the layout of the windows contained within it.

It's also not necessary to reset the sizers in the button handler. Just
call the dialog's Fit again.

Thanks for the advice. A lot of my GUI work has just been imitating
previous code and never really learning the "right" way to do it -- if
it works, it works, but I suspect it's gotten me into weird edge cases
in the past.

--
Robin Dunn

-Chris

···

On Mon, Oct 22, 2012 at 8:13 PM, Robin Dunn <robin@alldunn.com> wrote: