views:

392

answers:

1

Following this tutorial on WxPython, I've noticed that in the Find/Replace Dialog example there are extra panels where it doesn't seem like they're actually doing anything. In fact, they seem to mess up even more the layout (though that is probably some mistake I made somewhere) For example, the tutorial has this code:

panel = wx.Panel(self, -1)

panel1 = wx.Panel(panel, -1)
grid1 = wx.GridSizer(2, 2)
grid1.Add(wx.StaticText(panel1, -1, 'Find: ', (5, 5)), 0,  wx.ALIGN_CENTER_VERTICAL)
grid1.Add(wx.ComboBox(panel1, -1, size=(120, -1)))
grid1.Add(wx.StaticText(panel1, -1, 'Replace with: ', (5, 5)), 0, wx.ALIGN_CENTER_VERTICAL)
grid1.Add(wx.ComboBox(panel1, -1, size=(120, -1)))

panel1.SetSizer(grid1)
vbox.Add(panel1, 0, wx.BOTTOM | wx.TOP, 9)

Why is that different from:

panel = wx.Panel(self, -1)

grid1 = wx.GridSizer(2, 2)
grid1.Add(wx.StaticText(panel, -1, 'Find: ', (5, 5)), 0,  wx.ALIGN_CENTER_VERTICAL)
grid1.Add(wx.ComboBox(panel, -1, size=(120, -1)))
grid1.Add(wx.StaticText(panel, -1, 'Replace with: ', (5, 5)), 0, wx.ALIGN_CENTER_VERTICAL)
grid1.Add(wx.ComboBox(panel, -1, size=(120, -1)))

vbox.Add(grid1, 0, wx.BOTTOM | wx.TOP, 9)

Note that I'm not creating a panel, just adding grid1 directly to vbox.

+1  A: 

I'm not sure. I've re-written the example into less code, although it's a bit hard to follow. I may e-mail this suggestions to her.

import wx

class FindReplace(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(255, 365))

        vbox_top = wx.BoxSizer(wx.VERTICAL)
        vbox = wx.BoxSizer(wx.VERTICAL)

        grid1 = wx.GridSizer(2, 2)
        grid1.Add(wx.StaticText(self, -1, 'Find: ', (5, 5)), 0,  wx.ALIGN_CENTER_VERTICAL)
        grid1.Add(wx.ComboBox(self, -1, size=(120, -1)))
        grid1.Add(wx.StaticText(self, -1, 'Replace with: ', (5, 5)), 0, wx.ALIGN_CENTER_VERTICAL)
        grid1.Add(wx.ComboBox(self, -1, size=(120, -1)))


        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer21 = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Direction'), orient=wx.VERTICAL)
        sizer21.Add(wx.RadioButton(self, -1, 'Forward', style=wx.RB_GROUP))
        sizer21.Add(wx.RadioButton(self, -1, 'Backward'))
        hbox2.Add(sizer21, 1, wx.RIGHT, 5)

        sizer22 = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Scope'), orient=wx.VERTICAL)
        # we must define wx.RB_GROUP style, otherwise all 4 RadioButtons would be mutually exclusive
        sizer22.Add(wx.RadioButton(self, -1, 'All', style=wx.RB_GROUP))
        sizer22.Add(wx.RadioButton(self, -1, 'Selected Lines'))
        hbox2.Add(sizer22, 1)


        sizer3 = wx.StaticBoxSizer(wx.StaticBox(self, -1, 'Options'), orient=wx.VERTICAL)
        vbox3 = wx.BoxSizer(wx.VERTICAL)
        grid = wx.GridSizer(3, 2, 0, 5)
        grid.Add(wx.CheckBox(self, -1, 'Case Sensitive'))
        grid.Add(wx.CheckBox(self, -1, 'Wrap Search'))
        grid.Add(wx.CheckBox(self, -1, 'Whole Word'))
        grid.Add(wx.CheckBox(self, -1, 'Incremental'))
        vbox3.Add(grid)
        vbox3.Add(wx.CheckBox(self, -1, 'Regular expressions'))
        sizer3.Add(vbox3, 0, wx.TOP, 4)


        sizer4 = wx.GridSizer(2, 2, 2, 2)
        sizer4.Add(wx.Button(self, -1, 'Find', size=(120, -1)))
        sizer4.Add(wx.Button(self, -1, 'Replace/Find', size=(120, -1)))
        sizer4.Add(wx.Button(self, -1, 'Replace', size=(120, -1)))
        sizer4.Add(wx.Button(self, -1, 'Replace All', size=(120, -1)))

        sizer5 = wx.BoxSizer(wx.HORIZONTAL)
        sizer5.Add((191, -1), 1, wx.EXPAND | wx.ALIGN_RIGHT)
        sizer5.Add(wx.Button(self, -1, 'Close', size=(50, -1)))


        vbox.Add(grid1, 0, wx.BOTTOM | wx.TOP, 9)        
        vbox.Add(hbox2, 0, wx.BOTTOM, 9)        
        vbox.Add(sizer3, 0, wx.BOTTOM, 15)        
        vbox.Add(sizer4, 0, wx.BOTTOM, 9)        
        vbox.Add(sizer5, 1, wx.BOTTOM, 9)
        vbox_top.Add(vbox, 1, wx.LEFT, 5)

        self.SetSizer(vbox_top)
        self.Centre()


app = wx.App(False)
d = FindReplace(None, -1, 'Find/Replace')
d.ShowModal()
d.Destroy()
app.MainLoop()
Steven Sproat
Well, thanks. BTW, I just noticed this: is the vbox_top necessary? It seems kinda redundant.
Javier Badia
Not really, seems it's only used to space the entire grid of the widgets 5px from the left of the window. I'm not a fan of all the -1 "id"s being used, but cba to change that :)That's one downside of wx, if you want to specify a certain border for a group of widgets, placing them in a sizer is the way to go.
Steven Sproat