wxpython无法切换面板

时间:2013-02-20 14:52:53

标签: wxpython

代码编译正常,只是在加载第二个面板时遇到问题。 我已经调查了许多关于面板切换的论坛,但它们似乎都没有帮助。

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

1 个答案:

答案 0 :(得分:0)

您的panel2panel1为父母。隐藏父项时,也会隐藏其子项。

此外,您没有为您的相框使用主分级器。这意味着一个是隐式创建的,第一个添加到框架(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()