While working with wxPython, it’s essential to grasp object orientation even if it hasn’t been the main focus of our series. This guide will help you understand how to design a tab interface with wxPython.
The Mainframe class constructs the frame, similar to previous examples. Other classes serve as the contents for the tabs. Within the main frame, we establish a panel and a notebook (which acts as the tab holder). Following that, we initiate tab objects:
tab1 = TabOne(nb) tab2 = TabTwo(nb) ...
We then attach these tab objects to the tab holder:
# Define the content of tabs as distinct classes: classTabOne(wx.Panel): def__init__(self, parent): wx.Panel.__init__(self, parent) t = wx.StaticText(self, -1, "Content of the first tab", (20,20))
classTabTwo(wx.Panel): def__init__(self, parent): wx.Panel.__init__(self, parent) t = wx.StaticText(self, -1, "Content of the second tab", (20,20))
classTabThree(wx.Panel): def__init__(self, parent): wx.Panel.__init__(self, parent) t = wx.StaticText(self, -1, "Content of the third tab", (20,20))
classTabFour(wx.Panel): def__init__(self, parent): wx.Panel.__init__(self, parent) t = wx.StaticText(self, -1, "Content of the fourth tab", (20,20))
classMainFrame(wx.Frame): def__init__(self): wx.Frame.__init__(self, None, title="wxPython tabbed interface example @pythonspot.com")
# Panel creation and tab holder setup: p = wx.Panel(self) nb = wx.Notebook(p)
# Initiation of the tab windows: tab1 = TabOne(nb) tab2 = TabTwo(nb) tab3 = TabThree(nb) tab4 = TabFour(nb)
# Assigning names to tabs and adding them: nb.AddPage(tab1, "Tab 1") nb.AddPage(tab2, "Tab 2") nb.AddPage(tab3, "Tab 3") nb.AddPage(tab4, "Tab 4")
# Organizing notebook layout using a sizer: sizer = wx.BoxSizer() sizer.Add(nb, 1, wx.EXPAND) p.SetSizer(sizer)
if __name__ == "__main__": app = wx.App() MainFrame().Show() app.MainLoop()
Leave a Reply: