3

I'm trying to use GTK3 and Cairo from Python for a minimal plotting application where the on-screen display of Cairo's output is for user convenience.

The typical usage is that I run a command, a plot pops up on screen and is also written to file, and I want to be able to dismiss the window as quickly as possible, ideally just a "q" keypress but also the common Ctrl-W and Ctrl-Q in addition to the default Alt-F4 (does anyone really use that regularly?!?).

I also want as little UI clutter in the window as possible: ideally just the standard window surround, no menus, toolbars, etc.

So... how can I bind my "q", "Ctrl-Q", etc. keybindings to Gtk.main_quit without having to a) create a cluttersome drop-down menu bar and b) go though the heavyweight Gtk.UIManager focused on by the Python Gtk+ 3 documentation here: http://python-gtk-3-tutorial.readthedocs.org/en/latest/menus.html . I hope this is possible, and doesn't require a lot of code (at least not as much as to set up all the menus!), but I can't find an example anywhere online: maybe I'm just searching for the wrong terms, being a GTK newbie.

Unfortunately there doesn't seem to be any documentation on making such a minimal accelerator setup, and the code to configure accelerator keys seems to differ a great deal between GTK2 and 3... thanks for helping.

drahnr
  • 6,782
  • 5
  • 48
  • 75
andybuckley
  • 1,114
  • 2
  • 11
  • 24
  • 1
    Keep in mind PyGTK (Gtk+ 2.x only) is something completely different from the `gi.repository` introspection bindings which are used (as you do) when doing Gtk+ 3 programs in python. – drahnr Apr 16 '14 at 16:27
  • Thanks for the clarification, I wasn't sure if they were different versions of the same project or entirely separate. Sounds more like the latter. – andybuckley Apr 16 '14 at 19:08

2 Answers2

3

Connect a signal to your main frame Win.connect('key-press-event', self.on_key_function) and in on_key_function (self, widget, event) check the value of event.keyval. For ESC is 65307 if you like hardcoded. Also, for key combinations, event.state report shift, alt(meta), and so on: if Gdk.ModifierType.CONTROL_MASK & event.state:do_something if ctrl is pressed You could have separate stataments for left-ctrl, right-alt; be sure not to try to capture predefined key combinations, thay may be consumed by window manager. A IDE with a good introspection will help you a lot, just write Gdk (previously imported) and autocompletion will popup a lot of functions/clases, most of them with a self-explanatory name.

cox
  • 731
  • 5
  • 12
  • Excellent, that's refreshingly compact compared to the tutorials which were all aimed at bigger things! – andybuckley Apr 16 '14 at 19:04
  • 1
    Bad recommendation. `event.keyval` is layout-dependent, such hotkeys won't work in non-Latin keyboard layouts. – user Jan 20 '17 at 17:39
  • I don't have time to check, but it was the „all-in-one” solution - the code was the same in windows/linux. And the layout was not identical (but yes, both latin). Feel free to update, I'm busy with house paintings – cox Jan 21 '17 at 09:17
0

Don't use key-press-event and keyval, it won't work for users with non-Latin keyboard layouts. GTK+ does a great job internally to match keyvals to hardware keys, this functionality is exposed via accelerators (often shortened as accel in the API) and bindings.

user
  • 23,260
  • 9
  • 113
  • 101
  • Maybe could show an example of how you would do that? – Thayne Jul 22 '18 at 19:42
  • @Thayne, sorry, not in Python, but for accelerators there's [an example in C](https://stackoverflow.com/a/19793067/3075942), hope it helps. – user Jul 30 '18 at 20:41