Python-Ref > GUI programming with PyGTK > Widgets > Notebook
 
 

<-^^->

Notebook

Tabbed widget for organization of GUI.
Notebook provides a widget for display of several pages over one another and tabs for selection of the pages.
Expand/Shrink
Zdroj: (gtk-notebook1-1.py) přispěl(a) Iva Forstová
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8       def __init__(self, title):
  9           self.window = gtk.Window()
 10           self.title = title
 11           self.window.set_title(title)
 12           self.window.set_size_request(300, -1)
 13           self.window.connect("destroy", self.destroy)
 14           self.create_interior()
 15           self.window.show_all()
 16           
 17       def create_interior(self):
 18           self.mainbox=gtk.VBox()
 19           self.table = gtk.Table(4, 6)
 20           self.window.add(self.mainbox)
 21           # create a notebook
 22           self.notebook = gtk.Notebook()
 23           # position the page indicators
 24           self.notebook.set_tab_pos(gtk.POS_LEFT)
 25           self.table.attach(self.notebook, 0, 6, 0, 1)
 26           self.notebook.show()
 27           self.table.show()
 28           # add or remove the notebook page tabs
 29           self.notebook.set_show_tabs(True)
 30           self.status_bar = gtk.Statusbar()
 31           self.status_bar.show()
 32           self.context_id = self.status_bar.get_context_id("Statusbar")
 33           self.status_bar.push(self.context_id, "Welcome to notebook!")
 34           # frame
 35           frame = gtk.Frame("Page 1")
 36           frame.show()
 37           img = gtk.Image()
 38           img.set_from_stock( gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_DIALOG)
 39           # add image to a frame
 40           frame.add(img)
 41           img.show()
 42           label = gtk.Label("Page 1")        
 43           # add page to a notebook 
 44           self.notebook.append_page(frame, label)
 45           # frame 2
 46           frame2 = gtk.Frame("Page 2")
 47           frame2.show()
 48           # label
 49           label2 = gtk.Label("Hello world!")
 50           # add label to a frame
 51           frame2.add(label2)
 52           label2.show()
 53           # new label, title page
 54           label2 = gtk.Label("Page 2")
 55           # add page to a notebook 
 56           self.notebook.prepend_page(frame2, label2)
 57           # frame 3
 58           frame3 = gtk.Frame("Page 3")
 59           frame3.show()
 60           # label
 61           label3 = gtk.Label("Hello world!")
 62           frame3.add(label3) 
 63           label3.show()
 64           label3 = gtk.Label("Page 3")
 65           # add page to a notebook 
 66           self.notebook.insert_page(frame3, label3, 1)
 67           # frame 4
 68           frame4 = gtk.Frame("Page 4")
 69           frame4.show()
 70           img4 = gtk.Image()
 71           img4.set_from_stock(gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
 72           frame4.add(img4)
 73           img4.show()
 74           label4 = gtk.Label("Page 4")
 75           # add page to a notebook 
 76           self.notebook.append_page(frame4, label4)
 77           # frame 5
 78           frame5 = gtk.Frame("Notebook")
 79           frame5.show()
 80           img5 = gtk.Image()
 81           img5.set_from_file("notebook.gif")
 82           frame5.add(img5)
 83           img5.show()
 84           label5 = gtk.Label("Notebook")
 85           # add page to a notebook 
 86           self.notebook.append_page(frame5, label5)
 87           self.notebook.set_current_page(2)
 88           # button - next page
 89           button_next = gtk.Button("Next page")
 90           button_next.connect("clicked", self.next_pages, self.context_id)
 91           self.table.attach(button_next, 0, 1, 1, 2)
 92           button_next.show()
 93           # button - previous page
 94           button_prev = gtk.Button("Previous page")
 95           button_prev.connect("clicked", self.prev_pages, self.context_id)
 96           self.table.attach(button_prev, 1, 2, 1, 2)
 97           button_prev.show()
 98           # button - remove a page
 99           button_remove = gtk.Button("Remove page")
100           button_remove.connect("clicked", self.remove_pages, self.context_id)
101           self.table.attach(button_remove, 2, 3, 1, 2)
102           button_remove.show()
103           # button - close
104           button_close = gtk.Button("Close")
105           button_close.connect("clicked", self.destroy)
106           self.table.attach(button_close, 3, 4, 1, 2)
107           button_close.show()
108           self.mainbox.pack_start(self.table)
109           self.mainbox.pack_end(self.status_bar)
110           
111       def next_pages(self, w, data):
112           # move to next page
113           self.notebook.next_page()
114           value = "Move to next page"
115           self.status_bar.push(data, value)
116           
117       def prev_pages(self, w, data):
118           # move to previous page
119           self.notebook.prev_page()
120           value = "Move to previous page"
121           self.status_bar.push(data, value)
122           
123       def remove_pages(self, w, data):
124           # find out the current page
125           page = self.notebook.get_current_page()
126           # remove a page from the notebook
127           self.notebook.remove_page(page)
128           value = "Remove a page from the notebook"
129           self.status_bar.push(data, value)
130           
131       def main(self):
132           gtk.main()
133     
134       def destroy(self, w):
135           print "Good bye..."
136           gtk.main_quit()
137           
138   if __name__ == "__main__":
139       m = MyGUI( "Notebook example")
140       m.main()
stdout:
Good bye...
Screenshot:
Program screenshot gtk-notebook1-1.pngProgram screenshot gtk-notebook1-1a.png
Doba běhu: 515.6 ms