修改由Glad设置的Gtk加速键集

2024-10-02 00:35:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Gtk(python3)+Glade创建一个应用程序。我在空地上设置了一些加速器,如下所示:

 <child>
     <object class="GtkImageMenuItem" id="imagemenuitem5">
         <property name="label">gtk-quit</property>
         <accelerator key="q" signal="activate" modifiers="GDK_CONTROL_MASK"/>
     </object>
 </child>

但我不知道如何在应用程序运行时将此事件的加速器更改为其他内容。有可能吗?我的实施是否与我的计划不符?在


Tags: nameidchild应用程序gtkobjectpropertylabel
1条回答
网友
1楼 · 发布于 2024-10-02 00:35:49

<accelerator>元素在内部使用^{}。根据该功能的文档:

Accelerators added through this function are not user changeable during runtime. If you want to support accelerators that can be changed by the user, use gtk_accel_map_add_entry() and gtk_widget_set_accel_path() or gtk_menu_item_set_accel_path() instead.

事实上,还有一种比这更现代的方法,使用GActionGApplication,和{},根本不需要创建任何菜单栏。以下是在运行时更改菜单项的加速器的示例:

from gi.repository import Gio, Gtk


class App(Gtk.Application):
    def __init__(self, **props):
        super(App, self).__init__(application_id='org.gnome.example',
            flags=Gio.ApplicationFlags.FLAGS_NONE, **props)
        # Activating the LEP GEX VEN ZEA menu item will rotate through these
        # accelerator keys
        self.keys = ['L', 'G', 'V', 'Z']

    def do_activate(self):
        Gtk.Application.do_activate(self)

        actions = self.create_actions()
        self.add_action(actions['quit'])

        self.win = Gtk.ApplicationWindow()
        self.add_window(self.win)
        self.win.add_action(actions['lep'])
        self.set_accels_for_action('win.lep', ['<primary>' + self.keys[0]])
        # Ctrl-Q is automatically assigned to an app action named quit

        model = self.create_menus()
        self.set_menubar(model)

        actions['lep'].connect('activate', self.on_lep_activate)
        actions['quit'].connect('activate', lambda *args: self.win.destroy())

        self.win.show_all()

    def create_actions(self):
        return {name: Gio.SimpleAction(name=name) for name in ['lep', 'quit']}

    def create_menus(self):
        file_menu = Gio.Menu()
        file_menu.append('LEP GEX VEN ZEA', 'win.lep')
        file_menu.append('Quit', 'app.quit')
        menu = Gio.Menu()
        menu.append_submenu('File', file_menu)
        return menu

    def on_lep_activate(self, *args):
        self.keys = self.keys[1:] + self.keys[:1]
        self.set_accels_for_action('win.lep', ['<primary>' + self.keys[0]])

if __name__ == '__main__':
    App().run([])

您也可以使用Glade文件来完成一些操作,并通过创建一个menus.ui文件并使其在特定的GResource路径上对您的应用程序可用来自动连接它:这被描述为here。在

相关问题 更多 >

    热门问题