Wed 9 Dec 2009
The “Book” Controls of wxPython (Part 2 of 2)
Posted by Mike under Cross-Platform, Python, wxPython
View Comments
Now we turn to changing the notebook’s style:
Listing 7
def onChangeTheme(self, event): """ Changes the notebook's theme Note: Based partially on the agw AUI demo """ evId = self.themeDict[event.GetString()] all_panes = self.aui_mgr.GetAllPanes() for pane in all_panes: if isinstance(pane.window, aui.AuiNotebook): nb = pane.window if evId == ID_NotebookArtGloss: nb.SetArtProvider(aui.AuiDefaultTabArt()) elif evId == ID_NotebookArtSimple: nb.SetArtProvider(aui.AuiSimpleTabArt()) elif evId == ID_NotebookArtVC71: nb.SetArtProvider(aui.VC71TabArt()) elif evId == ID_NotebookArtFF2: nb.SetArtProvider(aui.FF2TabArt()) elif evId == ID_NotebookArtVC8: nb.SetArtProvider(aui.VC8TabArt()) elif evId == ID_NotebookArtChrome: nb.SetArtProvider(aui.ChromeTabArt()) nb.Refresh() nb.Update()
The event handler is called from the first toolbar’s combobox events. It too grabs the user’s choice via event.GetString() and then uses the string as a key for my theme dictionary. The dictionary returns an integer which is assigned to “evId”. Next, the AuiManager instance calls GetAllPanes() to get a list of all the pages in the notebook. Finally, the handler then loops over the pages and uses a nested conditional to change the notebook’s them the call to SetArtProvider(). To show the changes, we finish by calling the notebook’s Refresh and Update methods.
The last method that I’m going to go over from this demo is “onDisableTab”:
Listing 8
def onDisableTab(self, event): """ Disables the current tab """ page = self.notebook.GetCurrentPage() page_idx = self.notebook.GetPageIndex(page) self.notebook.EnableTab(page_idx, False) self.notebook.AdvanceSelection()
This event handler gets fired by a menu event and is a pretty simple piece of code. First, we call the notebook’s GetCurrentPage() method and then pass the result to the notebook’s GetPageIndex() method. Now that we have the page index, we can use that to disable it via the notebook’s EnableTab method. As you can see, by passing False, we disable the page. You can also use the EnableTab method to re-enable the tab by passing True.
Wrapping Up
There are tons of other methods that affect the behavior of both of these notebooks. It would take several more articles to cover everything. Be sure to download the wxPython demo and the SVN version of the code to get the most of these wonderful notebooks and to see what I haven’t covered here. For example, I didn’t talk about the events of the respective widgets, the tab position (bottom, top, etc), or the many and varied abilities that can lock down the AuiNotebook. Also note that the AuiNotebook supports “perspectives”. The official demo has an example, so I didn’t replicate it here. Remember that both the FlatNotebook and the AGW AuiNotebook are pure python, so you can hack at them yourself if you know python.
Note: All code tested on Windowx XP / Vista with wxPython 2.8.10.1 (unicode) and Python 2.5 using the latest SVN versions of AGW. The code should work equally well on other operating systems. If not, let me know of email the wxPython mailing list.
Further Reading
Downloads
-
mld
-
bc