Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 12827  / 2 Years ago, thu, july 14, 2022, 10:10:08

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?


More From » python

 Answers
1

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.".


[#37437] Friday, July 15, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kneducator

Total Points: 226
Total Questions: 111
Total Answers: 108

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
kneducator questions
Tue, Dec 6, 22, 09:22, 1 Year ago
Sun, Apr 30, 23, 23:26, 1 Year ago
Tue, Jun 8, 21, 01:25, 3 Years ago
Sun, Mar 12, 23, 10:51, 1 Year ago
;