Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am creating an application for the Ubuntu App Showdown in Python + Gtk with quickly. I am calling it Discvur. I would have liked to include some keyboard shortcuts.

Searching the web, I found this page about AccelGroup and this page about gtk_widget_add_accelerator but I don't understand how to start with it. Could you tell me how I would define an appropriate signal and then use it in my application?

share|improve this question

2 Answers 2

Here's some bits of code from one of my Python + Gtk apps, further extended according to the comments to this answer:

self.my_accelerators = Gtk.AccelGroup()
self.entry = Gtk.builder.get_object("entry1")
self.add_accelerator(self.entry, "<Control>b", signal="backspace")
...

def add_accelerator(self, widget, accelerator, signal="activate"):
    """Adds a keyboard shortcut"""
    if accelerator is not None:
        #if DEBUG:
            #print accelerator, widget.get_tooltip_text()
        key, mod = Gtk.accelerator_parse(accelerator)
        widget.add_accelerator(signal, self.my_accelerators, key, mod, Gtk.AccelFlags.VISIBLE)
share|improve this answer
    
Thank you but I got this error: self.add_accelerator(self.quick_add_entry, "<Control>l", signal="grab-focus") AttributeError: 'DiscvurWindow' object has no attribute 'quick_add_entry'. When I try a web search of "quick_add_entry", I cannot find it anywhere. Are you sure it is the right function? Where can I find more information/a tutorial for this? Additionally, does "<Control>l" mean Ctrl + l or is it a mistype? –  Agmenor Jun 20 '12 at 18:01
    
It's not a function, that's just the name of whatever widget you're connecting the accelerator to. I've revised it to be clearer. –  Bryce Jun 20 '12 at 19:06
    
Thank you for your answer. First, I took the liberty to change the "gtk" into "Gtk". Second, when I run your code I get "NameError: global name 'DEBUG' is not defined", so I suppose this is a variable from your own script. So I commented it in my own script. Third, after having commented it, I get the error "AttributeError: 'DiscvurWindow' object has no attribute 'accelerators'". I am sorry if this is supposed to be evident but the documentation is still obscure to me; in particular the documentation says add_accelerator needs 6 arguments? –  Agmenor Jun 21 '12 at 3:18
    
Right, you need to create accelerators yourself; I've updated the example to show how. –  Bryce Jun 21 '12 at 6:21
    
After a little research and failed tries, I could get a working script. Could you please verify it? If you think it is correct, I will (or you can) modify your answer: paste.ubuntu.com/1053516. I would appreciate your review a lot, Bryce. "A little research" was a euphemism, but I suppose it is better to learn a man how to fish than giving him fish. So thank you again. –  Agmenor Jun 22 '12 at 0:36
up vote 5 down vote accepted

Here is the code that finally worked. As it depends highly on my development environment Quickly + Glade + Python + Gtk, I make it an independent answer. Bryce's answer helped a lot, and so did my exchanges with aking1012.

The actual code, in a text editor:

# Accelerators
self.my_accelerators = Gtk.AccelGroup()
self.window = self.builder.get_object("discvur_window")
self.window.add_accel_group(self.my_accelerators)
self.entry = self.builder.get_object("entry1")
self.add_accelerator(self.entry, "<Control>b", signal="backspace")

…

def add_accelerator(self, widget, accelerator, signal="activate"):
    """Adds a keyboard shortcut"""
    if accelerator is not None:
        #if DEBUG:
            #print accelerator, widget.get_tooltip_text()
        key, mod = Gtk.accelerator_parse(accelerator)
        widget.add_accelerator(signal, self.my_accelerators, key, mod, Gtk.AccelFlags.VISIBLE)
        print "The accelerator is well added with the signal " + signal

def on_erasing(self, widget):
    print "It works."

In Glade, I created a GtkEntry called "entry1" in my window called "discvur_window". In the 'Signals' tab, I gave the signal "backspace" a handler called "on_erasing".

Now, hitting Backspace or Ctrl+B makes the terminal print "It works.".

share|improve this answer
    
Can this be used to disable Ctrl+F4 shortcut to close the window? –  Juzer Ali Sep 3 '12 at 17:04
    
You mean Alt+F4 ? I don't know, I haven't tried. –  Agmenor Sep 3 '12 at 20:35
    
I meant both Ctrl+F4 and Alt+F4 –  Juzer Ali Sep 4 '12 at 13:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.