BoxSizer: how to adjust SizerItems after manually setting size of the windows controlled by the sizer

Hi all,

I have some windows that are controlled by a BoxSizer.

I manually set the size and position of a sub-window controlled by the BoxSizer with a sub-window.SetRect call.

After that, when I resize the frame, the size of the sub-windows are restored to the original sizes.

I tried to adjust the proprtions of the SizerItems of the BoxSizer to new values based on the new sizes of the sub-windows.

But this does works sub-optimal because I loose some information.

In my example the button1 is initially set with a proportion of 0.

After adjusting the proportions, the value 0 is lost.

What is the best method to adjust the SizerItems in this case ?

Example-Code (press button1 to change sizes):

···

import wx

class TestFrame(wx.Frame):

def init(self):

wx.Frame.init(self, None, -1, “Test”)

panel = wx.Panel(self, -1)

self.sizer = wx.BoxSizer(wx.VERTICAL)

panel.SetSizer(self.sizer)

self.button0 = wx.Button(panel, 0, “Button1”)

self.sizer.Add(self.button0, 0, wx.EXPAND)

self.button1 = wx.Button(panel, 0, “Button1 - Click me to increase”)

self.sizer.Add(self.button1, 1, wx.EXPAND)

self.button2 = wx.Button(panel, 0, “Button2”)

self.sizer.Add(self.button2, 1, wx.EXPAND)

self.button1.Bind(wx.EVT_BUTTON, self.OnButton)

def IncButton1(self, distance):

increase height of button 1

self.ChangeHeight(self.button1, +distance)

decrease height of button 2 and move down button2

self.ChangeHeight(self.button2, -distance)

self.MoveVertical(self.button2, +distance)

def ChangeHeight(self, window, distance):

rect = window.GetRect()

rect.SetHeight(rect.GetHeight() + distance)

window.SetRect(rect)

def MoveVertical(self, window, distance):

rect = window.GetRect()

rect.SetY(rect.GetY() + distance)

window.SetRect(rect)

def AdjustSizerItems(self):

sizer_items = []

idx = 0

while True:

try:

sizer_items.append(self.sizer.GetItem(idx))

idx += 1

except:

break

height = 0

for sizer_item in sizer_items:

height += sizer_item.GetWindow().GetRect().GetHeight()

proportions = []

for sizer_item in sizer_items:

proportion = 1000 * float(sizer_item.GetWindow().GetRect().GetHeight()) / height

proportions.append(proportion)

print proportions

for sizer_item, proportion in zip(sizer_items, proportions):

sizer_item.SetProportion(proportion)

def OnButton(self, event):

self.IncButton1(10)

self.AdjustSizerItems()

event.Skip()

app = wx.PySimpleApp()

TestFrame().Show()

app.MainLoop()


Erwin

I think I would simply ignore items that have a proportion=0 and only adjust the others. That is the way sizers work when you have some items with proportions and others without, it is the free space that is still available after handling the proportion==0 items that the other items get a share of.

···

On 9/22/12 10:29 AM, ErwinP wrote:

Hi all,

I have some windows that are controlled by a BoxSizer.
I manually set the size and position of a sub-window controlled by the
BoxSizer with a sub-window.SetRect call.
After that, when I resize the frame, the size of the sub-windows are
restored to the original sizes.
I tried to adjust the proprtions of the SizerItems of the BoxSizer to
new values based on the new sizes of the sub-windows.
But this does works sub-optimal because I loose some information.
In my example the button1 is initially set with a proportion of 0.
After adjusting the proportions, the value 0 is lost.
What is the best method to adjust the SizerItems in this case ?

--
Robin Dunn
Software Craftsman