我为什么要制作多个wx.Panel?

时间:2010-01-16 00:26:33

标签: python wxpython

关于WxPython的this tutorial,我注意到在查找/替换对话框示例中有额外的面板,看起来它们实际上并没有做任何事情。实际上,它们似乎更加混乱了布局(虽然这可能是我在某处做的一些错误)例如,教程有这样的代码:

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)

为什么与以下不同:

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)

请注意,我没有创建面板,只是将grid1直接添加到vbox

1 个答案:

答案 0 :(得分:1)

我不确定。我已经将示例重新编写为更少的代码,尽管它有点难以理解。我可以通过电子邮件将此建议发送给她。

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()