5

I am in the process of creating my first Gtk3+ application using python, and I am quite frustrated with the lack of documentation :(

Well, what I'm trying to do is to configure the Delete button to remove an item from my TreeView, and I don't have a remove button in order to bind the "clicked" signal of it for my code.

This is my code:

def make_shortcuts(self):
        self.my_accelerators = Gtk.AccelGroup()
        self.add_accel_group(self.my_accelerators)
        self.add_accelerator(self.treeview, "<Delete>", signal="???")

def add_accelerator(self, widget, accelerator, signal="activate"):
        if accelerator is not None:
            key, mod = Gtk.accelerator_parse(accelerator)
            widget.add_accelerator(signal, self.my_accelerators, key, mod, Gtk.AccelFlags.VISIBLE)

I don't get how a keyboard shortcut must be binded to a signal and not to a function. Binding to a function is way more flexible.

Is there any way I can bind a keyboard shortcut to a function?

4
  • What do you mean by not having a remove button?
    – Anonimista
    Nov 26, 2013 at 11:51
  • If I did have a remove button for my list, I could easily bind the Delete button to the "clicked" signal of my button, which is binded to the delete function and problem solved. So, I don't have and I don't want to have such a button. I just want when I press the Delete button the function that would run IF I had a remove button to run.
    – hytromo
    Nov 26, 2013 at 13:08
  • What does your delete function look like?
    – Anonimista
    Nov 26, 2013 at 15:25
  • @Anonimista This is completely irrelevant.
    – hytromo
    Nov 26, 2013 at 18:39

1 Answer 1

5

Instead of using accelerators, you can connect the key-press-event signal of your TreeView to a function and check if it was the Delete key that was pressed.

def on_treeview_key_press_event(self, widget, event):
    key = Gdk.keyval_name(event.keyval)
    if key == 'Delete':
        self.remove_items()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .