Re: Stock items and accelerators



>item = gtk.ImageMenuItem(action.stock_id, accel_group)

This also works fine with my gtk-2.4 and pygtk-2.2

import gtk

def menuitem_cb(widget, window):
    dialog = gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
                               gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
                               'Action activated')
    dialog.connect('response', lambda dialog, response: dialog.destroy())
    dialog.show()

def itemfactory_cb(window, action, widget):
    dialog = gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
                               gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
                               'You selected or toggled the menu item: '
                               '"%s"' % (gtk.item_factory_path_from_widget(widget),))
    dialog.connect('response', lambda dialog, response: dialog.destroy())
    dialog.show()

menu_items = (
    ('/_Gtk',        None,  itemfactory_cb,	     0, '<StockItem>', "demo-gtk-logo"),
    )

def register_stock_icons ():
    items = [('demo-gtk-logo', '_GTK!', gtk.gdk.CONTROL_MASK, ord('g'), ''),
    ('demo-python-logo', '_Python!', gtk.gdk.CONTROL_MASK, ord('p'), '')]
    
    # Register our stock items
    gtk.stock_add (items)
    
    # Add our custom icon factory to the list of defaults
    factory = gtk.IconFactory ()
    factory.add_default ()
    
    pixbuf = gtk.gdk.pixbuf_new_from_file ('gtk-logo-rgb.gif')
    pixbuf = pixbuf.add_alpha(True, chr(0xff), chr(0xff), chr(0xff))

    # Register icon to accompany stock item
    if pixbuf:
	icon_set = gtk.IconSet (pixbuf)
	factory.add ('demo-gtk-logo', icon_set)
	factory.add ('demo-python-logo', icon_set)
    else:
	print 'failed to load GTK logo for toolbar'
	

def main():
    register_stock_icons ()
    
    # Create the toplevel window
    window = gtk.Window()
    window.set_title('Application Window')
    window.connect('destroy', lambda win: gtk.main_quit())

    # Create the menubar

    accel_group = gtk.AccelGroup()
    window.add_accel_group(accel_group)
    
    menu_bar = gtk.MenuBar ();
    
    # create menu items from factory

    menuitem = gtk.MenuItem('_Foo')
    item_factory = gtk.ItemFactory(gtk.Menu, '<main>', accel_group)
    item_factory.create_items(menu_items, window)
    menu = item_factory.get_widget('<main>')
    menuitem.set_submenu(menu)
    menu_bar.add(menuitem)

    # create item by hand
    
    menuitem = gtk.MenuItem('_Bar')
    menu = gtk.Menu()
    menuitem.set_submenu(menu)
    menu_bar.add(menuitem)

    menuitem = gtk.ImageMenuItem ("demo-python-logo", accel_group)
    menu.add (menuitem)
    menuitem.connect ("activate", menuitem_cb, window);
    menuitem.show()
    
    window.add (menu_bar)
   
    window.show_all()

    gtk.main()

if __name__ == '__main__':
    main()




>What am I missing?

Note, that if you are using UIManager from pygtk-2.4 you need to get accel group from manager
with method get_accel_group and attach it to toplevel window. Don't create accel group manually.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]