How to Visually Float a Control
If you need to position an object so that it remains its natural size, but floats centered in an area, you can use dSizers and spacers to achieve this.
For example, let's say that you want a label centered horizontally on a form. You'd first create a horizontal sizer, and then add the label surrounded by spacers. The trick is to give the spacers proportions of 1 and the label a proportion of 0. Here's what the code would look like, assuming that self is the form:
lbl = dabo.ui.dLabel(self, Caption="I'm in the middle!") sz = dabo.ui.dSizer("h") sz.appendSpacer(1, proportion=1) sz.append(lbl, proportion=0) sz.appendSpacer(1, proportion=1) self.Sizer.append(sz, "expand") self.layout()
In this example we instructed the form's sizer (
self.Sizer) to expand
sz to take up all available width.
Another option would have been:
This would have left
sz using its minimal size and just positioned the whole
sz sizer in the center of
Neat trick - When running an application in which you are using the dabo base menu (the default) there is the option under View to Show Sizer Lines. This makes a big difference in trying to see what is happening with the instructions you are providing.
Another option, assuming that you have a single vertical sizer for the form, is to add the label, being sure to set the
halign parameter when adding to the sizer:
lbl = dabo.ui.dLabel(self, Caption="I'm in the middle!") self.Sizer.append(lbl, halign="center") self.layout()
This is like the previous example but without using the second sizer.