以下python Gtk应用程序代码在显示一个简单菜单方面非常有效。可以使用快捷键(aka accelerator / accel)激活该菜单。但是钥匙并没有显示在菜单旁边。是什么引起了这个?我在Ubuntu 12.04 64位上使用Gtk 3.4,Python 2.7进行了测试。
from gi.repository import Gio, Gtk
import sys
menus_bar_xml ="""
<?xml version="1.0"?>
<interface>
<menu id="menubar">
<submenu>
<attribute name="label">Menu</attribute>
<section>
<item>
<attribute name="label">Gtk</attribute>
<attribute name="action">app.version</attribute>
<attribute name="accel">v</attribute>
</item>
</section>
</submenu>
</menu>
</interface>
"""
class Application(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id="bk2suz.motion_picture")
self.connect("startup", self.on_startup)
self.connect("activate", self.on_activate)
def on_startup(self, app):
builder = Gtk.Builder()
builder.add_from_string(menus_bar_xml)
self.set_menubar(builder.get_object('menubar'))
action = Gio.SimpleAction.new("version", None)
action.connect('activate', self.version_callback)
self.add_action(action)
def on_activate(self, app):
win = Gtk.ApplicationWindow()
win.set_default_size(640, 480)
win.set_title('Sample')
self.add_window(win)
win.show_all()
def version_callback(self, action, parameter):
print('Gtk Version {0}.{1}'.format(Gtk.get_major_version(), Gtk.get_minor_version()))
if __name__ == '__main__':
app = Application()
app.run(sys.argv)