【问题标题】:How to get multiple panels on one frame when overwriting the standard frame?覆盖标准框架时如何在一帧上获得多个面板?
【发布时间】:2012-11-17 11:18:40
【问题描述】:

我正在学习来自http://wiki.wxpython.org/Getting%20Started 的教程,并且正忙于笔记本示例。在这里,多个面板被添加到一个框架中。它使用以下示例代码:

class ExamplePanel(wx.Panel):
    def __init__(self, parent):
        [...]

app = wx.App(False)
frame = wx.Frame(None, title="Demo with Notebook")
nb = wx.Notebook(frame)

nb.AddPage(ExamplePanel(nb), "Absolute Positioning")
nb.AddPage(ExamplePanel(nb), "Page Two")
nb.AddPage(ExamplePanel(nb), "Page Three")
frame.Show()
app.MainLoop()

这对我有用。但是,我不想使用标准的 wx.Frame() 作为框架,而是使用自己的框架(如果我只使用一个面板,框架就可以工作):

class ExampleFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self,parent, title = title)
        panel = wx.Panel(self)
        self.CreateStatusBar()
        # Setting up the menu.
        filemenu = wx.Menu()

        #wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About", " Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", " Terminate the program")

        # Creating the menubar
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&miP3") # Adding filemenu to the MenuBar
        self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.

        # Set events
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)     

    def OnAbout(self, event):
        # A message dialoge box with an OK button. wx.OK is a sandard ID in wxWidgets
        dlg = wx.MessageDialog(self, "Author: Niek de Klein", "About miP3")
        dlg.ShowModal() # show it
        dlg.Destroy() # finally destroy it when finished

    def OnExit(self, eevent):
        self.Close(True) # close the frame

app = wx.App(False)
frame = frame = ExampleFrame(None, title="Demo with Notebook")
nb = wx.Notebook(frame)

nb.AddPage(ExamplePanel(nb), "Absolute Positioning")
nb.AddPage(ExamplePanel(nb), "Page Two")
nb.AddPage(ExamplePanel(nb), "Page Three")
frame.Show()
app.MainLoop()

如何将多个面板添加到我的自定义框架中?

【问题讨论】:

    标签: python wxpython panel frame


    【解决方案1】:

    你可以这样做:

    #!/usr/bin/env python
    import wx
    import wx.grid
    
    class ExampleFrame ( wx.Frame ):
    
        def __init__( self, parent ):
            wx.Frame.__init__ ( self, parent)
    
            self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
    
            bSizer1 = wx.BoxSizer( wx.VERTICAL )
    
            self.Panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
            bSizer2 = wx.BoxSizer( wx.VERTICAL )
    
            self.nb = wx.Notebook( self.Panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
    
            bSizer2.Add( self.nb, 1, wx.EXPAND |wx.ALL, 5 )
    
            self.Panel.SetSizer( bSizer2 )
            self.Panel.Layout()
            bSizer2.Fit( self.Panel )
            bSizer1.Add( self.Panel, 1, wx.EXPAND |wx.ALL, 0 )
    
            self.SetSizer( bSizer1 )
            self.Layout()
            self.menuBar = wx.MenuBar( 0 )
            self.filemenu = wx.Menu()
            self.menuAbout = wx.MenuItem( self.filemenu, wx.ID_ANY, u"&About", u" Information about this program", wx.ITEM_NORMAL )
            self.filemenu.AppendItem( self.menuAbout )
    
            self.menuExit = wx.MenuItem( self.filemenu, wx.ID_ANY, u"E&xit", u" Terminate the program", wx.ITEM_NORMAL )
            self.filemenu.AppendItem( self.menuExit )
    
            self.menuBar.Append( self.filemenu, u"&miP3" )
    
            self.SetMenuBar( self.menuBar )
    
            self.statusBar = self.CreateStatusBar( 1, wx.ST_SIZEGRIP, wx.ID_ANY )
    
            self.Centre( wx.BOTH )
    
            self.Bind( wx.EVT_MENU, self.OnAbout, id = self.menuAbout.GetId() )
            self.Bind( wx.EVT_MENU, self.OnExit, id = self.menuExit.GetId() )
    
        def __del__( self ):
                pass
    
        def OnAbout( self, event ):
            event.Skip()
    
        def OnExit( self, event ):
            event.Skip()
    
    class ExamplePanel ( wx.Panel ):
    
        def __init__( self, parent ):
            wx.Panel.__init__ ( self, parent )
    
            # whatever you want on your panel goes here, for example a grid
            bSizer3 = wx.BoxSizer( wx.VERTICAL )
            self.m_grid1 = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_grid1.CreateGrid( 5, 3 )
            self.m_grid1.EnableEditing( True )
            self.m_grid1.EnableGridLines( True )
            self.m_grid1.EnableDragGridSize( False )
            self.m_grid1.SetMargins( 0, 0 )
            self.m_grid1.EnableDragColMove( False )
            self.m_grid1.EnableDragColSize( True )
            self.m_grid1.SetColLabelSize( 30 )
            self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
            self.m_grid1.EnableDragRowSize( True )
            self.m_grid1.SetRowLabelSize( 80 )
            self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
            self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
            bSizer3.Add( self.m_grid1, 1, wx.ALL|wx.EXPAND, 5 )
            self.SetSizer( bSizer3 )
            self.Layout()
    
        def __del__( self ):
            pass
    
    class MyFrame( ExampleFrame ):
        def __init__( self, parent ):
            ExampleFrame.__init__( self, parent )
    
            self.title = "Demo with Notebook"
            self.SetTitle(self.title)
    
            self.nb.AddPage(ExamplePanel(self.nb), "Absolute Positioning")
            self.nb.AddPage(ExamplePanel(self.nb), "Page Two")
            self.nb.AddPage(ExamplePanel(self.nb), "Page Three")
    
        def OnAbout(self, event):
            # A message dialoge box with an OK button. wx.OK is a sandard ID in wxWidgets
            dlg = wx.MessageDialog(self, "Author: Niek de Klein", "About miP3")
            dlg.ShowModal() # show it
            dlg.Destroy() # finally destroy it when finished
    
        def OnExit(self, eevent):
            self.Close(True) # close the frame
    
    class testapp(wx.App):
        def OnInit(self):
            self.m_frame = MyFrame(None)
            self.m_frame.Show()
            self.SetTopWindow(self.m_frame)
            return True
    
    app = testapp(0)
    app.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多