开发者

How to add keyboard navigation to a menu?

开发者 https://www.devze.com 2023-02-26 05:45 出处:网络
How can I add keyboard navigation (using Alt with underlines to suggest what other keys to use) to a py开发者_运维技巧thon gtk gobject-introspection application.

How can I add keyboard navigation (using Alt with underlines to suggest what other keys to use) to a py开发者_运维技巧thon gtk gobject-introspection application.

This code works for showing a simple menu but does not add keyboard navigation:

mb = Gtk.MenuBar()
filemenu = Gtk.Menu()
filem = Gtk.MenuItem()
filem.set_label("File")
filem.set_submenu(filemenu)    
closem = Gtk.MenuItem()
closem.show()
closem.set_label("Close")
closem.connect("activate", Gtk.main_quit)
filemenu.append(closem)
mb.append(filem)

How can I change it to allow keyboard navigation?


Set the use-underline property and prepend _ to the key you want to use as shortcut.

close_menu = Gtk.MenuItem()
close_menu.set_label("_Close")
close_menu.set_use_underline(True)

If your version of PyGObject is recent enough, you can also use

close_menu = Gtk.MenuItem("_Close", True)
0

精彩评论

暂无评论...
验证码 换一张
取 消