mihir bhatt
mihir bhatt

Reputation: 19

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

Here i have a doubt that when notebook tab is clicked at that time only it should load class in that tab. But in wxpython all class loads by default in tab so how can i put conditions to load class when tab is clicked. Here is a small example.

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

Here I want to display static text when the tab is clicked otherwise class should not load.

Upvotes: 0

Views: 237

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

You might want a different method of selecting what is and what is not displayed in the notebook.
A menu, for example, seems an appropriate selection tool.
i.e.

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

enter image description here

Upvotes: 1

Related Questions