How to create a notebook (Phoenix)
Keywords : Notebook, Frame, Panel, Page, Tab.
Contents
Introduction :
wx.Notebook widget joins multiple windows with corresponding tabs.
You can position the Notebook widget using the following style flags :
- wx.NB_LEFT
- wx.NB_RIGHT
- wx.NB_TOP
- wx.NB_BOTTOM
The default position is wx.NB_TOP.
wx.Notebook methods |
|
integer GetRowCount() |
get the number of rows |
SetPadding(wx.Size padding) |
set the amount of space around each page's icon and label |
SetTabSize(wx.Size size) |
set the tab size |
(tab, where) HitTest(wx.Point point) |
return the tab which is hit |
wx.Size CalcSizeFromPage(wx.Size size) |
something |
add bookcontrolbase methods where return value of the HitTest() method can be one of :
- wx.NB_HITTEST_NOWHERE
- wx.NB_HITTEST_ONITEM
(info by ZetCode / Jan Bodnar).
Demonstrating :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
First example
A simple example of using a wx.Notebook to implement a tabbed interface with 3 page windows.
1 # sample_one.py
2
3 import wx
4
5 # class MyPageOne
6 # class MyPageTwo
7 # class MyPageThree
8 # class MyFrame
9 # class MyApp
10
11 #---------------------------------------------------------------------------
12
13 class MyPageOne(wx.Panel):
14 def __init__(self, parent):
15 wx.Panel.__init__(self, parent)
16
17 #------------
18
19 self.SetBackgroundColour(wx.Colour("Green"))
20
21 #------------
22
23 txt = wx.StaticText(self, -1,
24 """This is a "MyPageOne" object""",
25 (20, 20))
26
27 #---------------------------------------------------------------------------
28
29 class MyPageTwo(wx.Panel):
30 def __init__(self, parent):
31 wx.Panel.__init__(self, parent)
32
33 #------------
34
35 self.SetBackgroundColour(wx.Colour("Yellow"))
36
37 #------------
38
39 txt = wx.StaticText(self, -1,
40 """This is a "MyPageTwo" object""",
41 (40, 40))
42
43 #---------------------------------------------------------------------------
44
45 class MyPageThree(wx.Panel):
46 def __init__(self, parent):
47 wx.Panel.__init__(self, parent)
48
49 #------------
50
51 self.SetBackgroundColour(wx.Colour("Light Gray"))
52
53 #------------
54
55 txt = wx.StaticText(self, -1,
56 """This is a "MyPageThree" object""",
57 (60, 60))
58
59 #---------------------------------------------------------------------------
60
61 class MyFrame(wx.Frame):
62 def __init__(self, parent, id, title):
63 wx.Frame.__init__(self, parent, id, title)
64
65 self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
66
67 #------------
68
69 # Here we create a panel and a notebook on the panel.
70 pnl = wx.Panel(self)
71 nb = wx.Notebook(pnl)
72
73 #------------
74
75 # Ccreate the page windows as children of the notebook.
76 page1 = MyPageOne(nb)
77 page2 = MyPageTwo(nb)
78 page3 = MyPageThree(nb)
79
80 #------------
81
82 # Add the pages to the notebook with the label to show on the tab.
83 nb.AddPage(page1, "Page 1")
84 nb.AddPage(page2, "Page 2")
85 nb.AddPage(page3, "Page 3")
86
87 #------------
88
89 # Finally, put the notebook in a sizer for
90 # the panel to manage the layout.
91 sizer = wx.BoxSizer()
92 sizer.Add(nb, 1, wx.EXPAND)
93 pnl.SetSizer(sizer)
94
95 #---------------------------------------------------------------------------
96
97 class MyApp(wx.App):
98 def OnInit(self):
99
100 #------------
101
102 frame = MyFrame(None, -1,
103 "Simple notebook example")
104 self.SetTopWindow(frame)
105 frame.Show(True)
106
107 return True
108
109 #---------------------------------------------------------------------------
110
111 def main():
112 app = MyApp(redirect=False)
113 app.MainLoop()
114
115 #---------------------------------------------------------------------------
116
117 if __name__ == "__main__" :
118 main()
Second example
In our example, we mimic the look of a spreadsheet.
1 # sample_two.py
2
3 """
4
5 Author : Jan Bodnar
6 Website : zetcode.com
7
8 """
9
10 import wx
11 import wx.grid
12 #from wx.lib import sheet
13
14 # class MySheet
15 # class MyFrame
16 # class MyApp
17
18 #---------------------------------------------------------------------------
19
20 class MySheet(wx.grid.Grid):
21 def __init__(self, *args, **kw):
22 super(MySheet, self).__init__(*args, **kw)
23
24 self.SetLabelBackgroundColour('#DBD4D4')
25
26 #------------
27
28 self.InitUI()
29
30 #-----------------------------------------------------------------------
31
32 def InitUI(self):
33 nOfRows = 55
34 nOfCols = 25
35
36 self.row = self.col = 0
37 self.CreateGrid(nOfRows, nOfCols)
38
39 self.SetColLabelSize(20)
40 self.SetRowLabelSize(50)
41
42 #---------------------------------------------------------------------------
43
44 class MyFrame(wx.Frame):
45 def __init__(self, *args, **kw):
46 super(MyFrame, self).__init__(*args, **kw)
47
48 self.InitUI()
49
50 #-----------------------------------------------------------------------
51
52 def InitUI(self):
53 self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
54 self.SetTitle("SpreadSheet")
55 self.SetSize((550, 550))
56
57 #------------
58
59 menuBar = wx.MenuBar()
60
61 file = wx.Menu()
62 file.Append(101, 'Quit', '' )
63 menuBar.Append(file, "&File")
64 self.SetMenuBar(menuBar)
65
66 self.Bind(wx.EVT_MENU, self.OnQuit, id=101)
67
68 #------------
69
70 notebook = wx.Notebook(self, style=wx.RIGHT)
71
72 sheet1 = MySheet(notebook)
73 sheet2 = MySheet(notebook)
74 sheet3 = MySheet(notebook)
75 sheet1.SetFocus()
76
77 notebook.AddPage(sheet1, 'Sheet1')
78 notebook.AddPage(sheet2, 'Sheet2')
79 notebook.AddPage(sheet3, 'Sheet3')
80
81 #------------
82
83 box = wx.BoxSizer(wx.VERTICAL)
84 box.Add(notebook, 1, wx.EXPAND)
85
86 self.SetSizer(box)
87
88 #------------
89
90 self.StatusBar()
91
92 #------------
93
94 self.Centre()
95
96 #-----------------------------------------------------------------------
97
98 def StatusBar(self):
99 self.statusbar = self.CreateStatusBar()
100
101
102 def OnQuit(self, event):
103 self.Close()
104
105 #---------------------------------------------------------------------------
106
107 class MyApp(wx.App):
108 def OnInit(self):
109 frame = MyFrame(None)
110 frame.Centre()
111 frame.Show(True)
112
113 return True
114
115 #---------------------------------------------------------------------------
116
117 def main():
118 app = MyApp(redirect=False)
119 app.MainLoop()
120
121 #---------------------------------------------------------------------------
122
123 if __name__ == "__main__" :
124 main()
Download source
Additional Information
Link :
https://wiki.wxpython.org/TitleIndex
Thanks to
??? (sample_one.py coding), Jan Bodnar (sample_two.py coding), the wxPython community...
About this page
Date(d/m/y) Person (bot) Comments :
12/01/20 - Ecco (Updated page for wxPython Phoenix).
Comments
- blah, blah, blah....