Is there a best practice for automatically adjusting the size of a window?

In the attached example.py script, how can I make the window adjust its size after the size of the sizer?

My initial attempt was to get the size of the sizer with GetSize() and then adjust the size of the window with SetSize() before the window is shown on line 8. But that won’t work as GetSize() on the sizer returns (0, 0) before it’s shown on line 8. Is it possible to get the size of the sizer before the window is shown?

More general, is there a best practice how to do automatic size adjustments in wxPython? I could of course just manually adjust the size. But across platforms and computers I rather have wxPython do that for me automatically than use hardcoded sizes.

example.py (1.97 KB)

You can try to bind a wx.EVT_SIZING on the sizer and adjust the window size proportional to the sizer size.

example:

def OnSizing(self,event):
xNew,yNew = sizer.GetSize()
x = xNew / self.xOld
y = yNew / self.yOld
windowx, windowy = self.GetSize()
self.SetSize((windowxx,windowyy))
self.xOld = xNew
self.yOld = yNew

I think it must work so, but i didnt test this.

This is what I use.

         self.Fit()
         self.SetMinSize(self.GetSize())

The first line does what you are asking for and the second one prevents e.g. a dialog to be sized smaller then what it needs to show all its controls (using up their minimum space).

Werner

···

On 09/15/2011 05:45 AM, �ke Kullenberg wrote:

In the attached example.py script, how can I make the window adjust its size after the size of the sizer?

My initial attempt was to get the size of the sizer with GetSize() and then adjust the size of the window with SetSize() before the window is shown on line 8. But that won't work as GetSize() on the sizer returns (0, 0) before it's shown on line 8. Is it possible to get the size of the sizer before the window is shown?

More general, is there a best practice how to do automatic size adjustments in wxPython? I could of course just manually adjust the size. But across platforms and computers I rather have wxPython do that for me automatically than use hardcoded sizes.

ah i see now i missunderstood you …

ignore my email from before.

That doesn't really work for me. If I put those two lines before
self.Show() the window is rendered extremely small, and if I put it
afterwards the window doesn't change size at all.

···

On Sep 15, 2:47 pm, werner <wbru...@free.fr> wrote:

On 09/15/2011 05:45 AM, ke Kullenberg wrote:

> In the attached example.py script, how can I make the window adjust
> its size after the size of the sizer?

> My initial attempt was to get the size of the sizer with GetSize() and
> then adjust the size of the window with SetSize() before the window is
> shown on line 8. But that won't work as GetSize() on the sizer returns
> (0, 0) before it's shown on line 8. Is it possible to get the size of
> the sizer before the window is shown?

> More general, is there a best practice how to do automatic size
> adjustments in wxPython? I could of course just manually adjust the
> size. But across platforms and computers I rather have wxPython do
> that for me automatically than use hardcoded sizes.

This is what I use.

     self\.Fit\(\)
     self\.SetMinSize\(self\.GetSize\(\)\)

The first line does what you are asking for and the second one prevents
e.g. a dialog to be sized smaller then what it needs to show all its
controls (using up their minimum space).

Werner

Yeap, didn't notice that there was a panel in there which wasn't in a sizer.

Added a sizer to the frame and add the panel to that sizer and then it works.

See attached
Werner

akesizing.py (2.15 KB)

···

On 09/15/2011 10:29 AM, c00kiemonster wrote:

On Sep 15, 2:47 pm, werner<wbru...@free.fr> wrote:

On 09/15/2011 05:45 AM, ke Kullenberg wrote:

In the attached example.py script, how can I make the window adjust
its size after the size of the sizer?
My initial attempt was to get the size of the sizer with GetSize() and
then adjust the size of the window with SetSize() before the window is
shown on line 8. But that won't work as GetSize() on the sizer returns
(0, 0) before it's shown on line 8. Is it possible to get the size of
the sizer before the window is shown?
More general, is there a best practice how to do automatic size
adjustments in wxPython? I could of course just manually adjust the
size. But across platforms and computers I rather have wxPython do
that for me automatically than use hardcoded sizes.

This is what I use.

          self.Fit()
          self.SetMinSize(self.GetSize())

The first line does what you are asking for and the second one prevents
e.g. a dialog to be sized smaller then what it needs to show all its
controls (using up their minimum space).

Werner

That doesn't really work for me. If I put those two lines before
self.Show() the window is rendered extremely small, and if I put it
afterwards the window doesn't change size at all.

Yup that worked as advertised. Thanks much!

···

On Sep 15, 5:37 pm, werner <wbru...@free.fr> wrote:

On 09/15/2011 10:29 AM, c00kiemonster wrote:

> On Sep 15, 2:47 pm, werner<wbru...@free.fr> wrote:
>> On 09/15/2011 05:45 AM, ke Kullenberg wrote:

>>> In the attached example.py script, how can I make the window adjust
>>> its size after the size of the sizer?
>>> My initial attempt was to get the size of the sizer with GetSize() and
>>> then adjust the size of the window with SetSize() before the window is
>>> shown on line 8. But that won't work as GetSize() on the sizer returns
>>> (0, 0) before it's shown on line 8. Is it possible to get the size of
>>> the sizer before the window is shown?
>>> More general, is there a best practice how to do automatic size
>>> adjustments in wxPython? I could of course just manually adjust the
>>> size. But across platforms and computers I rather have wxPython do
>>> that for me automatically than use hardcoded sizes.
>> This is what I use.

>> self.Fit()
>> self.SetMinSize(self.GetSize())

>> The first line does what you are asking for and the second one prevents
>> e.g. a dialog to be sized smaller then what it needs to show all its
>> controls (using up their minimum space).

>> Werner
> That doesn't really work for me. If I put those two lines before
> self.Show() the window is rendered extremely small, and if I put it
> afterwards the window doesn't change size at all.

Yeap, didn't notice that there was a panel in there which wasn't in a sizer.

Added a sizer to the frame and add the panel to that sizer and then it
works.

See attached
Werner

akesizing.py
2KViewDownload

In addition, if you ever just need to get the min size required by a sizer without using something like Fit that changes window size, then you can use sizer.CalcMin().

···

On 9/15/11 2:37 AM, werner wrote:

On 09/15/2011 10:29 AM, c00kiemonster wrote:

On Sep 15, 2:47 pm, werner<wbru...@free.fr> wrote:

On 09/15/2011 05:45 AM, ke Kullenberg wrote:

In the attached example.py script, how can I make the window adjust
its size after the size of the sizer?
My initial attempt was to get the size of the sizer with GetSize() and
then adjust the size of the window with SetSize() before the window is
shown on line 8. But that won't work as GetSize() on the sizer returns
(0, 0) before it's shown on line 8. Is it possible to get the size of
the sizer before the window is shown?
More general, is there a best practice how to do automatic size
adjustments in wxPython? I could of course just manually adjust the
size. But across platforms and computers I rather have wxPython do
that for me automatically than use hardcoded sizes.

This is what I use.

self.Fit()
self.SetMinSize(self.GetSize())

The first line does what you are asking for and the second one prevents
e.g. a dialog to be sized smaller then what it needs to show all its
controls (using up their minimum space).

Werner

That doesn't really work for me. If I put those two lines before
self.Show() the window is rendered extremely small, and if I put it
afterwards the window doesn't change size at all.

Yeap, didn't notice that there was a panel in there which wasn't in a
sizer.

Added a sizer to the frame and add the panel to that sizer and then it
works.

--
Robin Dunn
Software Craftsman