I have an application with a popup menu, which I activate by right
clicking on the main window. I'm trying to add keyboard accelerators. By
connecting the keypress event I can get the actions I want:

self.window.connect("key_press_event", self.keypress)

But of course these don't show up in the menu itself. So I've tried
playing with AccelGroup. The code I'm using is appended below. With it I
can get the accelerators to show in the menu, but they don't do anything
unless I also use the keypress event. I'm sure this is wrong as it
requires the keyboard accelerators to be defined in two places. 

Can anyone help or point me to some documentation? I've tried the
reference manual, but that has left me confused. Sadly the FAQ is silent
on this. 

I don't think an item factory will do what I want (I don't want a
menubar or an option menu), but I'm willing to be convinced otherwise.

Regards,

Chris
---------------
    def createaccelmenu(self,menuitems,func,top):
        accel=gtk.AccelGroup();
        menu = gtk.Menu()
        menu.set_accel_path("<main>/Popup")
        for line in menuitems:
            # menuitems is a list of tuples: (item,key)
            # key is e.g. 'a'
            (item,key) = line
            menuitem = gtk.MenuItem(item)
            menu.append(menuitem)
            if key:
                kv=ord(key)
                mod=gtk.gdk.CONTROL_MASK
                print kv,mod
                print gtk.accel_map_lookup_entry("<main>/Popup/"+item)
                gtk.accel_map_change_entry("<main>/Popup/"+item, kv, \
                                        mod, TRUE)
                print gtk.accel_map_lookup_entry("<main>/Popup/"+item)
                if func:                 
                    accel.connect("accel-activate",func,item)               
                    menuitem.connect("activate",func,item)           
        menuitem.show()            
        menu.show()
#         top.add_accel_group(accel)
        menu.set_accel_group(accel)
        self.menu=menu 
        return menu

# snipped from __init__

        menuitems=[
                ('Item A','a'),
                ('Item P','p'),
                ('Item X','x'),
                (None,None),
                ('Quit','q'),
                ]

        self.popup=createaccelmenu(menuitems,self.select,self.window) 

# and from the event handler

        elif event.button == 3:
            self.popup.popup( None, None, None, event.button,event.time)
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to