在 wxpython 中单击选项卡时,如何在笔记本中加载 class?

How can i load class in notebook when tab is clicked in wxpython?

我怀疑当时单击笔记本选项卡时,只有它应该在该选项卡中加载 class。但是在 wxpython 中,所有 class 默认情况下都加载在选项卡中,所以我如何设置条件以在单击选项卡时加载 class 。 这是一个小例子。

import wx

class tabclass(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is the help tab", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="notebook")
        mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
        self.nb = wx.Notebook(mainPanel,size=(1365, 700))
        tab0 = tabclass(self.nb)
        self.nb.AddPage(tab0, "Tab One")

if __name__ == "__main__":
    app = wx.App()  
    MainFrame().Show()
    app.MainLoop()
    

这里我想在单击选项卡时显示静态文本,否则 class 不应加载。

您可能需要一种不同的方法来选择笔记本中显示和不显示的内容。
例如,菜单似乎是合适的选择工具。

import wx

class tabclass(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is the help tab", (20,20))

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="notebook")
        mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
        self.nb = wx.Notebook(mainPanel,size=(1365, 700))

        tabmenu = wx.Menu()
        t1 = wx.MenuItem(tabmenu, id = -1, text="Help", kind=wx.ITEM_CHECK)
        tabmenu.Append(t1)
        t2 = wx.MenuItem(tabmenu, id = -1, text="Other tab", kind=wx.ITEM_CHECK)
        tabmenu.Append(t2)
        t3 = wx.MenuItem(tabmenu, id = -1, text="Another tab", kind=wx.ITEM_CHECK)
        tabmenu.Append(t3)
        tabmenu.Append(wx.ID_EXIT, '&Quit')

        # Creating the menubar.
        menuBar = wx.MenuBar()
        # Add menus
        menuBar.Append(tabmenu, "&Tabs")
        # Adding the MenuBar to the Frame content.
        self.SetMenuBar(menuBar)
        # Bind menu item to functions
        self.Bind(wx.EVT_MENU, self.helptab, t1)
        self.Bind(wx.EVT_MENU, self.othertab, t2)
        self.Bind(wx.EVT_MENU, self.anothertab, t3)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)

    # Obviously, here you would differentiate your tabs
    # I have used the same one for brevity
    def helptab(self, event):
        if event.IsChecked():
            self.tab0 = tabclass(self.nb)
            self.nb.AddPage(self.tab0, "Help Tab")
        else:
            #Delete the "Help Tab"
            pass

    def othertab(self, event):
        if event.IsChecked():
            self.tab1 = tabclass(self.nb)
            self.nb.AddPage(self.tab1, "Other Tab")
        else:
            #Delete the "Other Tab"
            pass

    def anothertab(self, event):
        if event.IsChecked():
            self.tab2 = tabclass(self.nb)
            self.nb.AddPage(self.tab2, "Another Tab")
        else:
            #Delete the "Another Tab"
            pass

    def OnQuit(self, event):
        self.Destroy()

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()