原文: https://pythonspot.com/wxpython-tabs/

    尽管出于简单性原因,我们并未在 wxPython 系列中大量使用面向对象,但是我们无法解决它。 在本教程中,您将学习如何使用 wxPython 创建选项卡界面。

    Mainframe类创建框架,就像前面的示例一样。 其他类别是选项卡的内容。 我们在主框架中创建一个面板和笔记本(标签夹)。 然后我们创建标签对象:

    1. tab1 = TabOne(nb)
    2. tab2 = TabTwo(nb)
    3. ...

    我们使用以下方法将其连接到标签夹:

    1. nb.AddPage(tab1, "Tab 1")
    2. nb.AddPage(tab2, "Tab 2")
    3. ...

    完整代码:

    1. import wx
    2. # Define the tab content as classes:
    3. class TabOne(wx.Panel):
    4. def __init__(self, parent):
    5. wx.Panel.__init__(self, parent)
    6. t = wx.StaticText(self, -1, "This is the first tab", (20,20))
    7. class TabTwo(wx.Panel):
    8. def __init__(self, parent):
    9. wx.Panel.__init__(self, parent)
    10. t = wx.StaticText(self, -1, "This is the second tab", (20,20))
    11. class TabThree(wx.Panel):
    12. def __init__(self, parent):
    13. wx.Panel.__init__(self, parent)
    14. t = wx.StaticText(self, -1, "This is the third tab", (20,20))
    15. class TabFour(wx.Panel):
    16. def __init__(self, parent):
    17. wx.Panel.__init__(self, parent)
    18. t = wx.StaticText(self, -1, "This is the last tab", (20,20))
    19. class MainFrame(wx.Frame):
    20. def __init__(self):
    21. wx.Frame.__init__(self, None, title="wxPython tabs example @pythonspot.com")
    22. # Create a panel and notebook (tabs holder)
    23. p = wx.Panel(self)
    24. nb = wx.Notebook(p)
    25. # Create the tab windows
    26. tab1 = TabOne(nb)
    27. tab2 = TabTwo(nb)
    28. tab3 = TabThree(nb)
    29. tab4 = TabFour(nb)
    30. # Add the windows to tabs and name them.
    31. nb.AddPage(tab1, "Tab 1")
    32. nb.AddPage(tab2, "Tab 2")
    33. nb.AddPage(tab3, "Tab 3")
    34. nb.AddPage(tab4, "Tab 4")
    35. # Set noteboook in a sizer to create the layout
    36. sizer = wx.BoxSizer()
    37. sizer.Add(nb, 1, wx.EXPAND)
    38. p.SetSizer(sizer)
    39. if __name__ == "__main__":
    40. app = wx.App()
    41. MainFrame().Show()
    42. app.MainLoop()

    输出:

    wxPython 选项卡 - 图1

    wx 选项卡