按下按钮添加页面(wx.Notebook)

时间:2015-03-06 08:23:09

标签: python wxpython wxnotebook

我有wx.Notebook和2页:

nb = wx.Notebook(PanelLobby,wx.ID_ANY,pos=(100,100),size=(413,214))
nb.AddPage(page1,"Page 1",select=True)
nb.AddPage(page2,"Page 2")

我想在按下按钮时添加页面, 我试图绑定一个wx.EVT_LEFT_DOWN事件,但没有运气。

提前感谢。

1 个答案:

答案 0 :(得分:0)

这实际上非常简单。这是一种方法:

import random
import wx

########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""        
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)
        self.tab_num = 3

        self.notebook = wx.Notebook(panel)
        tabOne = TabPanel(self.notebook)
        self.notebook.AddPage(tabOne, "Tab 1")

        tabTwo = TabPanel(self.notebook)
        self.notebook.AddPage(tabTwo, "Tab 2")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)

        btn = wx.Button(panel, label="Add Page")
        btn.Bind(wx.EVT_BUTTON, self.addPage)
        sizer.Add(btn)

        panel.SetSizer(sizer)
        self.Layout()

        self.Show()

    #----------------------------------------------------------------------
    def addPage(self, event):
        """"""
        new_tab = TabPanel(self.notebook)
        self.notebook.AddPage(new_tab, "Tab %s" % self.tab_num)
        self.tab_num += 1

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()