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

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

UIManager

Easier way to create menus and toolbars using UIManager.
The UIManager provides the ability to split the development of GUI from the internal workings of the application. It uses a simple XML based language to describe the Menu and Toobar of your application.
The following code is a simple rewrite of the Menu exaple.
Expand/Shrink
  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     ui = '''<ui>
 10       <menubar name="MenuBar">
 11         <menu action="Label">
 12           <menuitem action="Fill"/>
 13           <separator/>
 14           <menuitem action="Clear"/>
 15         </menu>
 16         <menu action="About" name="AboutMenu">
 17           <menuitem action="Version"/>
 18           <menuitem action="ShowAbout"/>
 19         </menu>
 20       </menubar>
 21       </ui>'''
 22   
 23     def __init__( self, title):
 24       self.window = gtk.Window()
 25       self.title = title
 26       self.window.set_title( title)
 27       self.window.set_size_request( 260, 100)
 28       self.window.connect( "destroy", self.destroy)
 29       self.create_interior()
 30       self.window.show_all()
 31   
 32     def create_interior( self):
 33       self.mainbox = gtk.VBox()
 34       self.window.add( self.mainbox)
 35       # create action group
 36       self.actiongroup = gtk.ActionGroup('MyActionGroup')
 37       self.actiongroup.add_actions(
 38         [('About', None, '_About'),
 39          ('Label', None, '_Label'),
 40          ('Clear', gtk.STOCK_CLEAR, '_Clear', None, 'Clear the label', self.clear_label),
 41          ('Fill',  gtk.STOCK_COLOR_PICKER, '_Fill',  None, 'Fill the label', self.fill_label),
 42          ('ShowAbout', gtk.STOCK_ABOUT, '_About', None, 'Show about information', self.show_about),
 43          ('Version', gtk.STOCK_INDEX, '_Version', None, 'Show version', self.show_version),
 44          ])
 45       # the uimanager
 46       self.uimanager = gtk.UIManager()
 47       accelgroup = self.uimanager.get_accel_group()
 48       self.window.add_accel_group( accelgroup)
 49   
 50       self.uimanager.insert_action_group( self.actiongroup, 0)
 51       self.uimanager.add_ui_from_string(self.ui)
 52       # menu bar
 53       self.menu_bar = self.uimanager.get_widget( "/MenuBar")
 54       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 55       self.menu_bar.show()
 56       # right justify the about menu
 57       self.uimanager.get_widget('/MenuBar/AboutMenu').set_right_justified( True)
 58       # label for answer
 59       self.answer_label = gtk.Label( "Hello menus")
 60       self.mainbox.pack_start( self.answer_label, padding=10)
 61       self.answer_label.show()
 62       # show the box
 63       self.mainbox.show()
 64   
 65     def main( self):
 66       gtk.main()
 67   
 68     def destroy( self, w):
 69       gtk.main_quit()
 70   
 71     def clear_label( self, w):
 72       self.answer_label.set_label( "")
 73   
 74     def fill_label( self, w):
 75       self.answer_label.set_label( "Hello")
 76   
 77     def show_about( self, w):
 78       self.answer_label.set_label( "Created by Beda Kosata")
 79   
 80     def show_version( self, w):
 81       self.answer_label.set_label( "2.1")
 82   
 83   if __name__ == "__main__":
 84     m = MyGUI( "UIManager example")
 85     m.main()
Screenshot:
Program screenshot gtk-uimanager1-1.pngProgram screenshot gtk-uimanager1-1a.png
Doba běhu: 1775.5 ms
The following code adds a toolbar.
Expand/Shrink
  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     ui = '''<ui>
 10       <menubar name="MenuBar">
 11         <menu action="Label">
 12           <menuitem action="Fill"/>
 13           <separator/>
 14           <menuitem action="Clear"/>
 15         </menu>
 16         <menu action="About" name="AboutMenu">
 17           <menuitem action="Version"/>
 18           <menuitem action="ShowAbout"/>
 19         </menu>
 20       </menubar>
 21       <toolbar name="Toolbar">
 22         <toolitem action="Fill"/>
 23         <toolitem action="Clear"/>
 24         <separator/>
 25         <toolitem action="Version"/>
 26         <toolitem action="ShowAbout"/>
 27       </toolbar>
 28       </ui>'''
 29   
 30     def __init__( self, title):
 31       self.window = gtk.Window()
 32       self.title = title
 33       self.window.set_title( title)
 34       self.window.set_size_request( 260, 160)
 35       self.window.connect( "destroy", self.destroy)
 36       self.create_interior()
 37       self.window.show_all()
 38   
 39     def create_interior( self):
 40       self.mainbox = gtk.VBox()
 41       self.window.add( self.mainbox)
 42       # create action group
 43       self.actiongroup = gtk.ActionGroup('MyActionGroup')
 44       self.actiongroup.add_actions(
 45         [('About', None, '_About'),
 46          ('Label', None, '_Label'),
 47          ('Clear', gtk.STOCK_CLEAR, '_Clear', None, 'Clear the label', self.clear_label),
 48          ('Fill',  gtk.STOCK_COLOR_PICKER, '_Fill',  None, 'Fill the label', self.fill_label),
 49          ('ShowAbout', gtk.STOCK_ABOUT, '_About', None, 'Show about information', self.show_about),
 50          ('Version', gtk.STOCK_INDEX, '_Version', None, 'Show version', self.show_version),
 51          ])
 52       # the uimanager
 53       self.uimanager = gtk.UIManager()
 54       accelgroup = self.uimanager.get_accel_group()
 55       self.window.add_accel_group( accelgroup)
 56   
 57       self.uimanager.insert_action_group( self.actiongroup, 0)
 58       self.uimanager.add_ui_from_string( self.ui)
 59       # menu bar
 60       self.menu_bar = self.uimanager.get_widget( "/MenuBar")
 61       self.mainbox.pack_start( self.menu_bar, expand=False, fill=False)
 62       self.menu_bar.show()
 63       # right justify the about menu
 64       self.uimanager.get_widget('/MenuBar/AboutMenu').set_right_justified( True)
 65       # toolbar
 66       toolbar = self.uimanager.get_widget('/Toolbar')
 67       self.mainbox.pack_start( toolbar, expand=False)
 68       # label for answer
 69       self.answer_label = gtk.Label( "Hello menus")
 70       self.mainbox.pack_start( self.answer_label, padding=10)
 71       self.answer_label.show()
 72       # show the box
 73       self.mainbox.show()
 74   
 75     def main( self):
 76       gtk.main()
 77   
 78     def destroy( self, w):
 79       gtk.main_quit()
 80   
 81     def clear_label( self, w):
 82       self.answer_label.set_label( "")
 83   
 84     def fill_label( self, w):
 85       self.answer_label.set_label( "Hello")
 86   
 87     def show_about( self, w):
 88       self.answer_label.set_label( "Created by Beda Kosata")
 89   
 90     def show_version( self, w):
 91       self.answer_label.set_label( "2.1")
 92   
 93   if __name__ == "__main__":
 94     m = MyGUI( "UIManager example")
 95     m.main()
Screenshot:
Program screenshot gtk-uimanager1-2.pngProgram screenshot gtk-uimanager1-2a.png
Doba běhu: 454.7 ms