views:

48

answers:

2

Ok I have an application I am coding and am trying to get a layout simpler to this: Sizer Alignment with all the input boxes aligned

Notice how the text is left justified and the input boxes are all aligned, I see this in the wxPython demo code, but they all use the flexgrid sizer and I am trying to only use BoxSizers (due to them being simpler and because I only understand a little of sizers and even struggle with using BoxSizers, in 6 months I would have an even harder time)

I have tried having the input and text in two vertical sizers and then putting those in a horizontal sizer, didn't work because the text was not aligned with the inputs. I also tried doing that and also having each text, input pairing in a sizer, even worse. Any suggestions?

A: 

For most layouts other than the most basic you usually can't escape using a number of different types of sizers in order to realize your design.

Here is a good tutorial on sizers.

volting
+1  A: 

Here's a simple example using just BoxSizers:

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        # create the labels
        lblOne = wx.StaticText(panel, label="labelOne", size=(60,-1))
        lblTwo = wx.StaticText(panel, label="lblTwo", size=(60,-1))
        lblThree = wx.StaticText(panel, label="lblThree", size=(60,-1))

        # create the text controls
        txtOne = wx.TextCtrl(panel)
        txtTwo = wx.TextCtrl(panel)
        txtThree = wx.TextCtrl(panel)

        # create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        lineOneSizer = wx.BoxSizer(wx.HORIZONTAL)
        lineTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
        lineThreeSizer = wx.BoxSizer(wx.HORIZONTAL)

        # add widgets to sizers
        lineOneSizer.Add(lblOne, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineOneSizer.Add(txtOne, 0, wx.ALL, 5)
        lineTwoSizer.Add(lblTwo, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineTwoSizer.Add(txtTwo, 0, wx.ALL, 5)
        lineThreeSizer.Add(lblThree, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineThreeSizer.Add(txtThree, 0, wx.ALL, 5)

        mainSizer.Add(lineOneSizer)
        mainSizer.Add(lineTwoSizer)
        mainSizer.Add(lineThreeSizer)

        panel.SetSizer(mainSizer)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

But this is kind of messy, so here's a refactored version:

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # create the main sizer
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Add a panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        lbls = ["labelOne", "lblTwo", "lblThree"]
        for lbl in lbls:
            self.buildLayout(lbl)
        self.panel.SetSizer(self.mainSizer)

    #----------------------------------------------------------------------
    def buildLayout(self, text):
        """"""
        lblSize = (60,-1)
        lbl = wx.StaticText(self.panel, label=text, size=lblSize)
        txt = wx.TextCtrl(self.panel)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        sizer.Add(txt, 0, wx.ALL, 5)
        self.mainSizer.Add(sizer)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
Mike Driscoll