Richard P. Muller wrote:

>I'm currently porting the python checkbook manager
>(pycheckbook.sourceforge.net) from the Tk widget set to PyGTK. I've
>found most of this work very straightforward, and really like the pygtk
>module a great deal. However, I'm having trouble with binding keys to
>commands. I would like to be able to bind, for example, Control-e to
>edit the current entry. In tk this is fairly straightforward, as I can
>use:
>       self.window.bind("<Control-e>",self.editentry)
>I've tried to use something analogous to how I bind commands to buttons,
>but
>       self.window.connect("<Control>e",self.editentry)
>
this method is for connecting handlers to signals.

>
>doesn't work, nor do several other variants of the key strokes.
>
>I was browsing through some GTK documentation, and I came upon keyboard
>accelerators. Is this what I want?
>
yes.

>
>
>I would be very grateful for any help that people can offer. Thanks!
>
Keybindings in gtk are managed by objects called GtkAccelGroups.  An 
accel group contains mappings from (key,modifiers) combinations to 
(object, signal) pairs.  Accel groups are associated with toplevel 
GtkWindows in a many-many relationship (so you can have accels that are 
only valid in a particular window, and another group of accels that 
apply to all windows, etc).

For gtk 1.2, accelerators can only be used to emit a signal on an 
object.  For 2.0, it is possible to simply connect a function to the key 
combo.  If you have handlers attached to the signals, then they will be 
called when the key combination is pressed.  Common signals to use are 
"activate" (for menu items, this is equivalent to selecting them, for 
entries the same as pressing enter), "clicked" (for buttons), 
"grab_focus" (this will move focus to the given widget).

So your code might look something like:
  accel_group = gtk.GtkAccelGroup()
  window.add_accel_group(accel_group)

  # as an example, make ctrl+e focus the widget "entry":
  entry.add_accelerator("grab_focus", accel_group, 'e', 
gtk.GDK.CONTROL_MASK, 0)

James.

-- 
Email: [EMAIL PROTECTED]
WWW:   http://www.daa.com.au/~james/



_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk

Reply via email to