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

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Menu

How to create an application menu.
Creating a menu might seem a little bit tedious at the first sight (and it does not improve at the second one).
Expand/Shrink
Zdroj: (gtk-menu1-1.py)
  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   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( 260, 100)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # the label menu
 22       label_menu = gtk.Menu()
 23       fill_item = gtk.MenuItem( "Fill")
 24       fill_item.connect( "activate", self.set_label, "Hello")
 25       fill_item.show()
 26       clear_item = gtk.MenuItem( "Clear")
 27       clear_item.connect( "activate", self.set_label, "")
 28       clear_item.show()
 29       label_menu.append( fill_item)
 30       label_menu.append( clear_item)
 31       # menu bar
 32       self.menu_bar = gtk.MenuBar()
 33       label_menu_item = gtk.MenuItem( "Label")
 34       label_menu_item.set_submenu( label_menu)
 35       self.menu_bar.append( label_menu_item)
 36       self.menu_bar.show()
 37       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 38       # label for answer
 39       self.answer_label = gtk.Label( "Hello menus")
 40       self.mainbox.pack_start( self.answer_label, padding=10)
 41       self.answer_label.show()
 42       # show the box
 43       self.mainbox.show()
 44   
 45     def main( self):
 46       gtk.main()
 47   
 48     def destroy( self, w):
 49       gtk.main_quit()
 50   
 51     def set_label( self, w, data=None):
 52       self.answer_label.set_label( data)
 53   
 54   if __name__ == "__main__":
 55     m = MyGUI( "Menu example")
 56     m.main()
Screenshot:
Program screenshot gtk-menu1-1.pngProgram screenshot gtk-menu1-1a.pngProgram screenshot gtk-menu1-1b.png
Doba běhu: 2002.8 ms
Expand/Shrink
Zdroj: (gtk-menu1-2.py)
  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   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( 260, 100)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # the label menu
 22       label_menu = gtk.Menu()
 23       fill_item = gtk.MenuItem( "Fill")
 24       fill_item.connect( "activate", self.set_label, "Hello")
 25       fill_item.show()
 26       clear_item = gtk.MenuItem( "Clear")
 27       clear_item.connect( "activate", self.set_label, "")
 28       clear_item.show()
 29       label_menu.append( fill_item)
 30       label_menu.append( gtk.SeparatorMenuItem())
 31       label_menu.append( clear_item)
 32       # the about menu
 33       about_menu = gtk.Menu()
 34       for name,text in [("Version","1.2"),("About","Created by Beda Kosata")]:
 35         item = gtk.MenuItem( name)
 36         item.connect( "activate", self.set_label, text)
 37         item.show()
 38         about_menu.append( item)
 39       # menu bar
 40       self.menu_bar = gtk.MenuBar()
 41       label_menu_item = gtk.MenuItem( "Label")
 42       label_menu_item.set_submenu( label_menu)
 43       self.menu_bar.append( label_menu_item)
 44       about_menu_item = gtk.MenuItem( "About")
 45       about_menu_item.set_right_justified( True)
 46       about_menu_item.set_submenu( about_menu)
 47       self.menu_bar.append( about_menu_item)
 48       self.menu_bar.show()
 49       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 50       # label for answer
 51       self.answer_label = gtk.Label( "Hello menus")
 52       self.mainbox.pack_start( self.answer_label, padding=10)
 53       self.answer_label.show()
 54       # show the box
 55       self.mainbox.show()
 56   
 57     def main( self):
 58       gtk.main()
 59   
 60     def destroy( self, w):
 61       gtk.main_quit()
 62   
 63     def set_label( self, w, data=None):
 64       self.answer_label.set_label( data)
 65   
 66   if __name__ == "__main__":
 67     m = MyGUI( "Menu example")
 68     m.main()
Screenshot:
Program screenshot gtk-menu1-2.pngProgram screenshot gtk-menu1-2a.pngProgram screenshot gtk-menu1-2b.png
Doba běhu: 1510.8 ms