Sizing flag

Hello everyone,

I have a vertical box sizer, to which I am adding a control at the top, then a few other things. I am using the wx.EXPAND flag, so that the control takes up all of the horizontal space. However when the control is a checkbox, it doesn't know how to expand, and just goes all the way to the right. This looks weird and actually makes it kind of hard to find the checkbox! If I use the flag wx.CENTER the checkbox is centered, but other controls don't expand then. I tried doing wx.EXPAND|wx.CENTER but it doesn't seem to respect the center. What can I do, any suggestions?

Thanks for your time,
Mike

Please send a small sample of running code. It will make it easier for us to help you. It will also be shorter than explaining it in English :).

Rooney, Mike (ext. 324) wrote:

···

Hello everyone,

I have a vertical box sizer, to which I am adding a control at the top, then a few other things. I am using the wx.EXPAND flag, so that the control takes up all of the horizontal space. However when the control is a checkbox, it doesn't know how to expand, and just goes all the way to the right. This looks weird and actually makes it kind of hard to find the checkbox! If I use the flag wx.CENTER the checkbox is centered, but other controls don't expand then. I tried doing wx.EXPAND|wx.CENTER but it doesn't seem to respect the center. What can I do, any suggestions?

Thanks for your time,
Mike

Please send a small sample of running code. It will make it easier for
us to help you. It will also be shorter than explaining it in English :).

Okay, here is a running example. It will show 4 modals total. Click
okay on each one to go to the next. The function in question is
SetControl at the top. The first three controls look fine, but
for the fourth one, the CheckBox is in the corner. I would like controls
that can't expand all the way to be centered. Thanks!

test.py (3.6 KB)

Rooney, Mike (ext. 324) wrote:

Please send a small sample of running code. It will make it easier for us to help you. It will also be shorter than explaining it in English :).

Okay, here is a running example. It will show 4 modals total. Click
okay on each one to go to the next. The function in question is
SetControl at the top. The first three controls look fine, but
for the fourth one, the CheckBox is in the corner. I would like controls
that can't expand all the way to be centered. Thanks!

The checkbox does expand just fine, you just can't see it because the (empty) label area is the same color as the panel's background. But in in any case, this change will do what you want.

     def SetControl(self, editControl, center=False):
         sizer = wx.BoxSizer(wx.VERTICAL)

         if center:
             sizer.Add(editControl, 1, wx.ALIGN_CENTER_HORIZONTAL)
         else:
             sizer.Add(editControl, 1, wx.EXPAND)
         sizer.AddSpacer(10)
         sizer.Add(self.optionsBox, 0, wx.CENTER)
         sizer.AddSpacer(10)
         sizer.Add(self.buttonSizer, 0, wx.CENTER)
         sizer.AddSpacer(10)

         self.SetSizerAndFit(sizer)

...

         dlg = LeafEditDialog(title='CheckBox')
         dlg.SetControl( wx.CheckBox(dlg, -1, "the label"), True )
         dlg.ShowModal()

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks, but is there maybe a more general solution? The example shows four pop-ups to demonstrate how the other controls correctly work, but in reality the method that calls SetControl has no idea of what the control is, it could be anything. I could do instance checking or something but that isn't a very attractive solution. The way it works in our application is the method creating the dialog calls a GetEditWidget method which returns a wx control, which it passes to the dialog via SetControl. I guess I could have the GetEditWidget also return a center flag, but I feel like there must a way for this to work. I can use it as a last resort though.

I tried myself doing nested sizers, one with EXPAND and one with ALIGN CENTER, and swapping which was which, but I could not get a solution. Either everything would stretch out and the checkbox would be to one side, or everything would be centered but the controls wouldn't stretch. I feel like I need a sizer that is told to grow as big as it, and then put that inside another sizer which centers the control but allows it to get big if it needs to. Maybe this is wrong. If I understand you correctly the checkbox also contains a label, like a StaticText, which it is applying the EXPAND to? That makes sense then and perhaps there is no other solution. It seems like a checkbox would exist without this, though. Perhaps not, oh well.

- Mike

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Thursday, February 08, 2007 2:53 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Re: Sizing flag

Rooney, Mike (ext. 324) wrote:

Please send a small sample of running code. It will make it easier for
us to help you. It will also be shorter than explaining it in English :).

Okay, here is a running example. It will show 4 modals total. Click
okay on each one to go to the next. The function in question is
SetControl at the top. The first three controls look fine, but
for the fourth one, the CheckBox is in the corner. I would like controls
that can't expand all the way to be centered. Thanks!

The checkbox does expand just fine, you just can't see it because the
(empty) label area is the same color as the panel's background. But in
in any case, this change will do what you want.

     def SetControl(self, editControl, center=False):
         sizer = wx.BoxSizer(wx.VERTICAL)

         if center:
             sizer.Add(editControl, 1, wx.ALIGN_CENTER_HORIZONTAL)
         else:
             sizer.Add(editControl, 1, wx.EXPAND)
         sizer.AddSpacer(10)
         sizer.Add(self.optionsBox, 0, wx.CENTER)
         sizer.AddSpacer(10)
         sizer.Add(self.buttonSizer, 0, wx.CENTER)
         sizer.AddSpacer(10)

         self.SetSizerAndFit(sizer)

...

         dlg = LeafEditDialog(title='CheckBox')
         dlg.SetControl( wx.CheckBox(dlg, -1, "the label"), True )
         dlg.ShowModal()

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Rooney, Mike (ext. 324) wrote:

Thanks, but is there maybe a more general solution? The example shows
four pop-ups to demonstrate how the other controls correctly work,
but in reality the method that calls SetControl has no idea of what
the control is, it could be anything. I could do instance checking or
something but that isn't a very attractive solution. The way it works
in our application is the method creating the dialog calls a
GetEditWidget method which returns a wx control, which it passes to
the dialog via SetControl. I guess I could have the GetEditWidget
also return a center flag, but I feel like there must a way for this
to work. I can use it as a last resort though.

Or you could have it pass back a sizer instead, and then just add that sizer to the main one. The control can be sized, aligned, whatever as needed within it's nested sizer.

If I understand you correctly the checkbox also contains a label,
like a StaticText, which it is applying the EXPAND to?

Yes, that is correct. Try setting the background colour of the checkbox widget to see better where it is located and how it is sized.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Or you could have it pass back a sizer instead, and then just add that
sizer to the main one. The control can be sized, aligned, whatever as
needed within it's nested sizer.

That is a clever idea! If it is a checkbox, instead of returning
the control directly, it puts the checkbox inside a sizer, centered,
and returns that sizer instead. This seems to work pretty well, thanks!