How to add icons to notebook tabs.
What Objects are Involved
- Create a notebook and add some panels to it.
- Load images into image list.
- Assign images in list to coresponding notebook page's tab.
I took a code snippet from an app I'm working on, so this is by no means a working program, just an example. Some things were taken from the wxPython demo. I added more comments to the code. What is assumed:
- You already have a wx.frame() class made
- There is a wx.notebook() created in the frame with name='notebook' (therefore callable within the class as self.notebook)
The sample function below should be run from init
1 def note_book(self): 2 # don't want to be typing 'self.' a bunch of times ;) 3 notebook = self.notebook 4 5 #make the notebook pages: 6 # in this case each notebook page (or panel) is its own class, they are too 7 # large to keep all in one file. The function called is 8 # the panel constructor (wx.Panel). They need to be passed the 'notebook' so 9 # they know what their parent is. 10 pickerPanel = picker.pickerPanel(notebook) 11 mainPanel = main.mainPanel(notebook) 12 numberPanel = numbering.numberingPanel(notebook) 13 DateTimePanel = DateTime.DateTimePanel(notebook) 14 errorPanel = errors.errorPanel(notebook) 15 16 #add notebook pages to notebook: 17 # 1st variable of 'AddPage' is previously described panel, 2nd 18 # variable is text to be displayed 19 notebook.AddPage(pickerPanel, 'Picker') 20 notebook.AddPage(mainPanel, '- Main -') 21 notebook.AddPage(numberPanel, 'Numbering') 22 notebook.AddPage(DateTimePanel, 'Date and Time ') 23 notebook.AddPage(errorPanel, 'Errors: 0 ') 24 25 #list containing notebook images: 26 # .ico seem to be more OS portable 27 il = wx.ImageList(16, 16) #the (16, 16) is the size in pixels of the images 28 img0 = il.Add(wx.Bitmap('art/icons/picker.ico', wx.BITMAP_TYPE_ICO)) 29 img1 = il.Add(wx.Bitmap('art/icons/main.ico', wx.BITMAP_TYPE_ICO)) 30 img2 = il.Add(wx.Bitmap('art/icons/numbering.ico', wx.BITMAP_TYPE_ICO)) 31 img3 = il.Add(wx.Bitmap('art/icons/date_time.ico', wx.BITMAP_TYPE_ICO)) 32 img4 = il.Add(wx.Bitmap('art/icons/errors.png', wx.BITMAP_TYPE_PNG)) 33 34 #set images to pages: 35 #first assign image list created above to notebook: 36 notebook.AssignImageList(il) 37 #then assign each image in list to coresponding page. 38 #the sharp-eyed will see you could use a loop for this, 39 #but for maximum clarity/understanding I'm using the long way... 40 notebook.SetPageImage(0, img0) 41 notebook.SetPageImage(1, img1) 42 notebook.SetPageImage(2, img2) 43 notebook.SetPageImage(3, img3) 44 notebook.SetPageImage(4, img4) 45 46 # if this isn't called the notebook background color doesn't work right when switching 47 # themes in XP. 48 self.notebook.SetBackgroundColour(self.notebook.GetThemeBackgroundColour())
ianaré sévi (ianare at gmail dot com)