更改Glad设置的Gtk加速器 - 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>

但是我看不到在应用程序运行时如何将此事件的加速器更改为其他功能。可能吗?我的实现对我打算做的事情有误吗?

参考方案

<accelerator>元素在内部使用 gtk_widget_add_accelerator() 。从该函数的文档中:

通过此功能添加的加速器在运行时用户不可更改。如果要支持可由用户更改的加速器,请改用gtk_accel_map_add_entry()gtk_widget_set_accel_path()gtk_menu_item_set_accel_path()

实际上,使用GActionGApplicationGMenuModel甚至还有一种更现代的方法,而无需创建任何菜单栏。这是在运行时更改菜单项的加速器的示例:

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进行了描述。

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

python JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…