10.12. Notebooks

The NoteBook Widget is a collection of "pages" that overlap each other; each page contains different information with only one page visible at a time. This widget has become more common lately in GUI programming, and it is a good way to show blocks of similar information that warrant separation in their display.

The first function call you will need to know, as you can probably guess by now, is used to create a new notebook widget.

  notebook = gtk.Notebook()

Once the notebook has been created, there are a number of methods that operate on the notebook widget. Let's look at them individually.

The first one we will look at is how to position the page indicators. These page indicators or "tabs" as they are referred to, can be positioned in four ways: top, bottom, left, or right.

  notebook.set_tab_pos(pos)

pos will be one of the following, which are pretty self explanatory:

  POS_LEFT
  POS_RIGHT
  POS_TOP
  POS_BOTTOM

POS_TOP is the default.

Next we will look at how to add pages to the notebook. There are three ways to add pages to a NoteBook. Let's look at the first two together as they are quite similar.

  notebook.append_page(child, tab_label)

  notebook.prepend_page(child, tab_label)

These methods add pages to the notebook by inserting them from the back of the notebook (append), or the front of the notebook (prepend). child is the widget that is placed within the notebook page, and tab_label is the label for the page being added. The child widget must be created separately, and is typically a set of options setup within one of the other container widgets, such as a table.

The final method for adding a page to the notebook contains all of the properties of the previous two, but it allows you to specify what position you want the page to be in the notebook.

  notebook.insert_page(child, tab_label, position)

The parameters are the same as append() and prepend() except it contains an extra parameter, position. This parameter is used to specify what place this page will be inserted into; the first page having position zero.

Now that we know how to add a page, lets see how we can remove a page from the notebook.

  notebook.remove_page(page_num)

This method takes the page specified by page_num and removes it from the widget pointed to by notebook.

To find out what the current page is in a notebook use the method:

  page = notebook.get_current_page()

These next two methods are simple calls to move the notebook page forward or backward. Simply provide the respective method call with the notebook widget you wish to operate on.

  notebook.next_page()

  notebook.prev_page()

Note

When the notebook is currently on the last page, and next_page() is called, nothing happens. Likewise, if the notebook is on the first page, and prev_page() is called, nothing happens.

This next method sets the "active" page. If you wish the notebook to be opened to page 5 for example, you would use this method. Without using this method, the notebook defaults to displaying the first page.

  notebook.set_current_page(page_num)

The next two methods add or remove the notebook page tabs and the notebook border respectively.

  notebook.set_show_tabs(show_tabs)

  notebook.set_show_border(show_border)

The next method is useful when the you have a large number of pages, and the tabs don't fit on the page. It allows the tabs to be scrolled through using two arrow buttons.

  notebook.set_scrollable(scrollable)

show_tabs, show_border and scrollable can be either TRUE or FALSE.

Now let's look at an example. The notebook.py program creates a window with a notebook and six buttons. The notebook contains 11 pages, added in three different ways, appended, inserted, and prepended. The buttons allow you rotate the tab positions, add or remove the tabs and border, remove a page, change pages in both a forward and backward manner, and exit the program. Figure 10.9, “Notebook Example” illustrates the program display:

Figure 10.9. Notebook Example

Notebook Example

The source code for notebook.py is:

    1	#!/usr/bin/env python
    2	
    3	# example notebook.py
    4	
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    8	
    9	class NotebookExample:
   10	    # This method rotates the position of the tabs
   11	    def rotate_book(self, button, notebook):
   12	        notebook.set_tab_pos((notebook.get_tab_pos()+1) %4)
   13	
   14	    # Add/Remove the page tabs and the borders
   15	    def tabsborder_book(self, button, notebook):
   16	        tval = False
   17	        bval = False
   18	        if self.show_tabs == False:
   19		    tval = True 
   20	        if self.show_border == False:
   21		    bval = True
   22	
   23	        notebook.set_show_tabs(tval)
   24	        self.show_tabs = tval
   25	        notebook.set_show_border(bval)
   26	        self.show_border = bval
   27	
   28	    # Remove a page from the notebook
   29	    def remove_book(self, button, notebook):
   30	        page = notebook.get_current_page()
   31	        notebook.remove_page(page)
   32	        # Need to refresh the widget -- 
   33	        # This forces the widget to redraw itself.
   34	        notebook.queue_draw_area(0,0,-1,-1)
   35	
   36	    def delete(self, widget, event=None):
   37	        gtk.main_quit()
   38	        return False
   39	
   40	    def __init__(self):
   41	        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   42	        window.connect("delete_event", self.delete)
   43	        window.set_border_width(10)
   44	
   45	        table = gtk.Table(3,6,False)
   46	        window.add(table)
   47	
   48	        # Create a new notebook, place the position of the tabs
   49	        notebook = gtk.Notebook()
   50	        notebook.set_tab_pos(gtk.POS_TOP)
   51	        table.attach(notebook, 0,6,0,1)
   52	        notebook.show()
   53	        self.show_tabs = True
   54	        self.show_border = True
   55	
   56	        # Let's append a bunch of pages to the notebook
   57	        for i in range(5):
   58	            bufferf = "Append Frame %d" % (i+1)
   59	            bufferl = "Page %d" % (i+1)
   60	
   61	            frame = gtk.Frame(bufferf)
   62	            frame.set_border_width(10)
   63	            frame.set_size_request(100, 75)
   64	            frame.show()
   65	
   66	            label = gtk.Label(bufferf)
   67	            frame.add(label)
   68	            label.show()
   69	
   70	            label = gtk.Label(bufferl)
   71	            notebook.append_page(frame, label)
   72	      
   73	        # Now let's add a page to a specific spot
   74	        checkbutton = gtk.CheckButton("Check me please!")
   75	        checkbutton.set_size_request(100, 75)
   76	        checkbutton.show ()
   77	
   78	        label = gtk.Label("Add page")
   79	        notebook.insert_page(checkbutton, label, 2)
   80	
   81	        # Now finally let's prepend pages to the notebook
   82	        for i in range(5):
   83	            bufferf = "Prepend Frame %d" % (i+1)
   84	            bufferl = "PPage %d" % (i+1)
   85	
   86	            frame = gtk.Frame(bufferf)
   87	            frame.set_border_width(10)
   88	            frame.set_size_request(100, 75)
   89	            frame.show()
   90	
   91	            label = gtk.Label(bufferf)
   92	            frame.add(label)
   93	            label.show()
   94	
   95	            label = gtk.Label(bufferl)
   96	            notebook.prepend_page(frame, label)
   97	    
   98	        # Set what page to start at (page 4)
   99	        notebook.set_current_page(3)
  100	
  101	        # Create a bunch of buttons
  102	        button = gtk.Button("close")
  103	        button.connect("clicked", self.delete)
  104	        table.attach(button, 0,1,1,2)
  105	        button.show()
  106	
  107	        button = gtk.Button("next page")
  108	        button.connect("clicked", lambda w: notebook.next_page())
  109	        table.attach(button, 1,2,1,2)
  110	        button.show()
  111	
  112	        button = gtk.Button("prev page")
  113	        button.connect("clicked", lambda w: notebook.prev_page())
  114	        table.attach(button, 2,3,1,2)
  115	        button.show()
  116	
  117	        button = gtk.Button("tab position")
  118	        button.connect("clicked", self.rotate_book, notebook)
  119	        table.attach(button, 3,4,1,2)
  120	        button.show()
  121	
  122	        button = gtk.Button("tabs/border on/off")
  123	        button.connect("clicked", self.tabsborder_book, notebook)
  124	        table.attach(button, 4,5,1,2)
  125	        button.show()
  126	
  127	        button = gtk.Button("remove page")
  128	        button.connect("clicked", self.remove_book, notebook)
  129	        table.attach(button, 5,6,1,2)
  130	        button.show()
  131	
  132	        table.show()
  133	        window.show()
  134	
  135	def main():
  136	    gtk.main()
  137	    return 0
  138	
  139	if __name__ == "__main__":
  140	    NotebookExample()
  141	    main()
  

I hope this helps you on your way with creating notebooks for your PyGTK applications.