More newbie sizer questions

Managed to get my widgets in approximately the right position, thanks to
“the book”, but still not quite right …! Firstly, I
want the text controls to be to the right of the window, secondly, the
borders round the component sizers seem to be added to the bottom of the
window rather than round the sizer.

For the first problem, I thought that datSizer would be resized within
dlgSizer and that the text control would appear at the RHS of the sizer,
but I obviously thought wrong. Adding

size=dlgSizer.GetSize()

datSizer.SetMinSize(size)

was supposed to reszie datSizer manually but this didn’t work
either. I guess the answer is straightforward but not to me …
:frowning:

As for the second problem, completely stumped here too! I want a
border of 10 pixels round datSizer and btnSizer so that they are centred
in the window but something is not right. On second thoughts here,
do I just have one problem related to the borders?

Thanks in advance!!!

Test Dialog Box with sizers

class MyDialog(wx.Dialog):

# Constructor

def __init__(self, parent, title):

    # Initialise base

class

    wx.Dialog.__init__(self,

parent, -1, title)

    # Define dialog box

controls

    d1Label = wx.StaticText(self,

-1, “Data item 1”)

    d1Text  =

wx.TextCtrl(self, value=“data_1_here”)

    d2Label = wx.StaticText(self,

-1, “Data item 2”)

    d2Text  =

wx.TextCtrl(self, value=“data_2_here”)

    d3Label = wx.StaticText(self,

-1, “Data item 3”)

    d3Text  =

wx.TextCtrl(self, value=“data_3_here”)

    d4Label = wx.StaticText(self,

-1, “Data item 4”)

    d4Text  =

wx.TextCtrl(self, value=“data_4_here”)

    okButton = wx.Button(self,

wx.ID_OK, “OK”)

    okButton.SetDefault()

    cancelButton = wx.Button(self,

wx.ID_CANCEL, “Cancel”)

    # Define sizers

    btnSizer =

wx.BoxSizer(wx.HORIZONTAL)

    dlgSizer =

wx.BoxSizer(wx.VERTICAL)

    datSizer =

wx.FlexGridSizer(cols = 2, hgap = 5, vgap = 5)

datSizer.AddGrowableCol(1) # Allow only
data input fields to grow

    # Add widgets to

sizers

    datSizer.Add(d1Label, 0,

wx.ALIGN_CENTER_VERTICAL)

    datSizer.Add(d1Text, 1,

wx.ALIGN_RIGHT)

    datSizer.Add(d2Label, 0,

wx.ALIGN_CENTER_VERTICAL)

    datSizer.Add(d2Text, 1,

wx.ALIGN_CENTER)

    datSizer.Add(d3Label, 0,

wx.ALIGN_CENTER_VERTICAL)

    datSizer.Add(d3Text, 1,

wx.ALIGN_CENTER)

    datSizer.Add(d4Label, 0,

wx.ALIGN_CENTER_VERTICAL)

    datSizer.Add(d4Text, 1,

wx.ALIGN_CENTER)

    btnSizer.Add((20,20),
  1. Space at LHS of buttons

    btnSizer.Add(okButton)

    btnSizer.Add((20,20),

  2. Space between buttons

btnSizer.Add(cancelButton)

    btnSizer.Add((20,20),
  1. Space at RHS of buttons

    dlgSizer.Add(datSizer, wx.ALL,
    border=10)

    dlgSizer.Add(btnSizer, wx.ALL,
    border=10)

    size=dlgSizer.GetSize()

datSizer.SetMinSize(size)

    # Fit frame according to

sizers’ needs

    self.SetSizer(dlgSizer)

    dlgSizer.Fit(self)

dlgSizer.SetSizeHints(self)

Alun Griffiths wrote:

        okButton = wx.Button(self, wx.ID_OK, "OK")
        okButton.SetDefault()
        cancelButton = wx.Button(self, wx.ID_CANCEL, "Cancel")

Well, I tried working with it, but I'm not good enough yet! Perhaps sizers behave differently with dialogs too, but I'm not sure.

But anyway, I thought I'd just mention a little thing I discovered recently about the above snippet. When you use pre-defined IDs, you don't have to specify the text label, because the ID carries this label with it, so you can write the above as:

okButton = wx.Button(self, wx.ID_OK)
okButton.SetDefault()
cancelButton = wx.Button(self, wx.ID_CANCEL)

"OK" and "Cancel" will still appear on the buttons.

Alun Griffiths wrote:

        dlgSizer.Add(datSizer, wx.ALL, border=10)
        dlgSizer.Add(btnSizer, wx.ALL, border=10)

You're missing a parameter in these Add calls, so you are not getting the wx.ALL in the right place. Try

          dlgSizer.Add(datSizer, 0, wx.ALL, border=10)
          dlgSizer.Add(btnSizer, 0, wx.ALL, border=10)

···

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