Python-Ref > GUI programming with PyGTK > Widgets > Context menu
 
 

<-^^->

Context menu

How to use Menu to create context menus.
Expand/Shrink
Zdroj: (gtk-menu5-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       self.answer_label = gtk.Label( "Hello menus")
 23       self.mainbox.pack_start( self.answer_label, padding=10)
 24       self.window.connect( 'button_release_event', self.popup_menu) 
 25       self.window.add_events( gtk.gdk.BUTTON_PRESS_MASK | 
 26                               gtk.gdk.BUTTON_RELEASE_MASK) 
 27       # status bar
 28       self.status = gtk.Statusbar()
 29       self.mainbox.pack_end( self.status, expand=False)
 30       # show the box
 31       self.mainbox.show_all() 
 32   
 33     def popup_menu( self, widget, event):
 34       if event.button == 3: 
 35         menu = self.create_context_menu() 
 36         menu.popup( None, None, None, event.button, event.get_time()) 
 37   
 38     def create_context_menu( self):
 39       context_menu = gtk.Menu()
 40       copy_item = gtk.MenuItem( "Copy")
 41       copy_item.connect( "activate", self.copy_label)
 42       copy_item.show()
 43       clear_item = gtk.MenuItem( "Clear")
 44       clear_item.connect( "activate", self.set_label, "")
 45       clear_item.show()
 46       def_item = gtk.MenuItem( "Set default")
 47       def_item.connect( "activate", self.set_label, "Hello menus")
 48       def_item.show()
 49       sep = gtk.SeparatorMenuItem()
 50       sep.show()
 51       context_menu.append( copy_item)
 52       context_menu.append( sep)
 53       context_menu.append( clear_item)
 54       context_menu.append( def_item)
 55       return context_menu
 56   
 57     def set_label( self, w, data=None):
 58       cid = self.status.get_context_id( "app")
 59       self.status.push( cid, "Setting Label to '%s'" % data)
 60       self.answer_label.set_label( data)
 61   
 62     def copy_label( self, w):
 63       clip = gtk.Clipboard()
 64       clip.set_text( self.answer_label.get_label())
 65       cid = self.status.get_context_id( "app")
 66       self.status.push( cid, "Label text copied")
 67   
 68     def main( self):
 69       gtk.main()
 70   
 71     def destroy( self, w):
 72       gtk.main_quit()
 73   
 74   
 75   if __name__ == "__main__":
 76     m = MyGUI( "Context menu example")
 77     m.main()
Screenshot:
Program screenshot gtk-menu5-1.pngProgram screenshot gtk-menu5-1a.png
Doba běhu: 1261.4 ms