wx.Listbook
The Listbook control uses a ListCtrl to instead of tabs to control the notebook. In this case, you can actually use labeled pictures to change tabs. It’s kind of weird, but I think it’s kind of cool too. As with the Choicebook, this control inherits from the BookCtrlBase and has the same methods. The primary differences seem to lie with the Listbook’s appearance and in its unique set of events. Let’s take a quick peek at my demo to see how to create one!
1 import images
2 import wx
3
4 ########################################################################
5 class TabPanel(wx.Panel):
6 """
7 This will be the first notebook tab
8 """
9 #----------------------------------------------------------------------
10 def __init__(self, parent):
11 """"""
12
13 wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
14
15 sizer = wx.BoxSizer(wx.VERTICAL)
16 txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
17 txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
18
19 sizer = wx.BoxSizer(wx.VERTICAL)
20 sizer.Add(txtOne, 0, wx.ALL, 5)
21 sizer.Add(txtTwo, 0, wx.ALL, 5)
22
23 self.SetSizer(sizer)
24
25
26 ########################################################################
27 class ListbookDemo(wx.Listbook):
28 """"""
29
30 #----------------------------------------------------------------------
31 def __init__(self, parent):
32 """Constructor"""
33 wx.Listbook.__init__(self, parent, wx.ID_ANY, style=
34 wx.BK_DEFAULT
35 #wx.BK_TOP
36 #wx.BK_BOTTOM
37 #wx.BK_LEFT
38 #wx.BK_RIGHT
39 )
40 # make an image list using the LBXX images
41 il = wx.ImageList(32, 32)
42 for x in range(3):
43 obj = getattr(images, 'LB%02d' % (x+1))
44 bmp = obj.GetBitmap()
45 il.Add(bmp)
46 self.AssignImageList(il)
47
48 pages = [(TabPanel(self), "Panel One"),
49 (TabPanel(self), "Panel Two"),
50 (TabPanel(self), "Panel Three")]
51 imID = 0
52 for page, label in pages:
53 self.AddPage(page, label, imageId=imID)
54 imID += 1
55
56 self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.OnPageChanged)
57 self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGING, self.OnPageChanging)
58
59 #----------------------------------------------------------------------
60 def OnPageChanged(self, event):
61 old = event.GetOldSelection()
62 new = event.GetSelection()
63 sel = self.GetSelection()
64 print 'OnPageChanged, old:%d, new:%d, sel:%d\n' % (old, new, sel)
65 event.Skip()
66
67 #----------------------------------------------------------------------
68 def OnPageChanging(self, event):
69 old = event.GetOldSelection()
70 new = event.GetSelection()
71 sel = self.GetSelection()
72 print 'OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel)
73 event.Skip()
74
75 ########################################################################
76 class DemoFrame(wx.Frame):
77 """
78 Frame that holds all other widgets
79 """
80
81 #----------------------------------------------------------------------
82 def __init__(self):
83 """Constructor"""
84 wx.Frame.__init__(self, None, wx.ID_ANY,
85 "Listbook Tutorial",
86 size=(700,400)
87 )
88 panel = wx.Panel(self)
89
90 notebook = ListbookDemo(panel)
91 sizer = wx.BoxSizer(wx.VERTICAL)
92 sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
93 panel.SetSizer(sizer)
94 self.Layout()
95
96 self.Show()
97
98 #----------------------------------------------------------------------
99 if __name__ == "__main__":
100 app = wx.PySimpleApp()
101 frame = DemoFrame()
102 app.MainLoop()
The Listbook’s “tab” controls can be set run along any of the sides, just like the wx.Notebook. You can also attach images with the same methods we used in the Notebook example at the beginning. I shortened the code up a bit by putting the panels in a list and looping over it, but other than that, there’s not much more to write about concerning this widget.
All code was tested on the following:
- Windows XP, wxPython 2.8.10.1 (unicode), Python 2.5
- Windows Vista, wxPython 2.8.10.1 (unicode), Python 2.5
For Further Information
Note: The tutorial on this page is a modified version of the tutorial on my blog