python logo

wx python


Python hosting: Host, run, and code Python in the cloud!

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.

Recommended Course: Creating GUI Applications 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:

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

Here’s the complete code for better clarity:

import wx

# Define the content of tabs as distinct classes:
class TabOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the first tab", (20,20))

class TabTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the second tab", (20,20))

class TabThree(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the third tab", (20,20))

class TabFour(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "Content of the fourth tab", (20,20))

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

Output Image:
wxTabs

Enrich your knowledge with: Creating GUI Applications with wxPython

Back | Next





Leave a Reply: