1

代码编译正常,只是无法加载第二个面板。我查看了许多有关面板切换的论坛,但似乎都没有帮助。

import wx

class Example(wx.Frame):
    def __init__(self, parent, title):
        ##super(Example, self).__init__(parent, title=title, 
        ##                                size=(500,  #width
        ##                                      300)) #height
        wx.Frame.__init__(self, None, size=(900,270))
        self.InitUI()
        self.Centre()
        self.Show()

    #define User Interface
    def InitUI(self):
        self.panel1 = wx.Panel(self, -1)

        #Menu bar
        menubar = wx.MenuBar()
        filem = wx.Menu()
        editm = wx.Menu()
        helpm = wx.Menu()

        menubar.Append(filem, '&File')
        menubar.Append(editm, '&Edit')
        menubar.Append(helpm,'&help')
        self.SetMenuBar(menubar)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        #buttons
        b1 = wx.Button(self.panel1, label='button', size=(300,30), pos=(0,0))
        hbox.Add(b1)
        b1.Bind(wx.EVT_BUTTON, self.OnB1)


        #panel2
        self.panel2 = wx.Panel(self.panel1, -1)
        self.panel2.Hide()
        vbox = wx.BoxSizer(wx.HORIZONTAL)
        b2 = wx.Button(self.panel2, label='button2', size=(300,30), pos=(0,0))
        vbox.Add(b2)

    def OnB1(self, event):
        self.panel2.Show()
        self.panel1.Hide()


if __name__ == '__main__':
    app = wx.App()
    Example(None, title='')
    app.MainLoop()
4

1 回答 1

0

panel2作为panel1父母。当你隐藏父母时,你也隐藏了它的孩子。

此外,您没有为您的框架使用主尺寸器。这意味着为您隐式创建了一个对象,并将添加到 frame ( panel1) 的第一个对象放入此 sizer 中。你panel2的不是不包含在sizer中,所以修复父母会导致panel2点击后可见,但不能正确调整大小。

您可以像这样修复它:

import wx

class Example(wx.Frame):
    def __init__(self, parent, title):
        ##super(Example, self).__init__(parent, title=title, 
        ##                                size=(500,  #width
        ##                                      300)) #height
        wx.Frame.__init__(self, None, size=(900,270))
        self.InitUI()
        self.Centre()
        self.Show()

    #define User Interface
    def InitUI(self):
        self.sizer = wx.BoxSizer() # Main window sizer

        self.panel1 = wx.Panel(self, -1)
        self.sizer.Add(self.panel1, 1, flag=wx.EXPAND)

        #Menu bar
        menubar = wx.MenuBar()
        filem = wx.Menu()
        editm = wx.Menu()
        helpm = wx.Menu()

        menubar.Append(filem, '&File')
        menubar.Append(editm, '&Edit')
        menubar.Append(helpm,'&help')
        self.SetMenuBar(menubar)

        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        #buttons
        b1 = wx.Button(self.panel1, label='button', size=(300,30), pos=(0,0))
        self.hbox.Add(b1)
        b1.Bind(wx.EVT_BUTTON, self.OnB1)
        self.panel1.SetSizer(self.hbox)


        #panel2
        self.panel2 = wx.Panel(self, -1)
        self.sizer.Add(self.panel2, 1, flag=wx.EXPAND)
        self.panel2.Hide()
        self.vbox = wx.BoxSizer(wx.HORIZONTAL)
        b2 = wx.Button(self.panel2, label='button2', size=(300,30), pos=(0,0))
        self.vbox.Add(b2)
        self.panel2.SetSizer(self.vbox)

        self.SetSizer(self.sizer) # Set main window sizer

    def OnB1(self, event):
        self.panel2.Show()
        self.panel1.Hide()
        self.sizer.Layout() # Force the main window sizer to recalculate

if __name__ == '__main__':
    app = wx.App(False)
    Example(None, title='')
    app.MainLoop()
于 2013-02-20T15:40:50.057 回答