如何在PyGI GTK中使用GIO Actions创建一个完整的菜单? [英] How to create a complete menu using GIO Actions in PyGI GTK?

查看:133
本文介绍了如何在PyGI GTK中使用GIO Actions创建一个完整的菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Gtk应用程序中转换菜单栏,以便它使用 GActions (来自Gio)而不是 GtkActions 在Python3中使用GObject Instrospection。



我一直试图自己弄清楚,但到目前为止似乎非常复杂,我没有太多的运气。



如果有人可以请示例如何创建一个简单的菜单 GAction 基于


  • 子菜单
  • 图标/热键

  • 带有非库存图标/热键的菜单项

  • 选中的菜单项

  • 和广播菜单项组

  • 禁用的(变灰)菜单项



  • 真正帮助我很多。



    编辑:这是我现在窗口中的菜单栏:





    如果有人可以复制使用GioActions显示的菜单项,那么我可以弄清楚它们是如何工作的,它会很棒。顺便说一下,所有的我使用窗口回调而不是应用程序回调,因此这是一个窗口菜单栏而不是应用程序菜单栏。

     #!/ usr / bin / env python3 

    #版权所有(C)2013 LiuLang < gsushzhsosgsu@gmail.com>

    #使用此源代码由GPLv3许可证管理,可在http://www.gnu.org/licenses/gpl-3.0.html中找到


    from gi.repository import gio
    from gi.repository import gtk
    import sys

    menus_str ='''
    <?xml version = 1.0\" >?;
    < interface>
    < menu id =appmenu>
    <节>
    < item>
    < attribute name =labeltranslatable =yes>首选项< / attribute>
    < attribute name =action> app.preferences< / attribute>
    < / item>
    < / section>
    <节>
    < item>
    < attribute name =labeltranslatable =yes>关于< / attribute>
    < attribute name =action> app.about< / attribute>
    < / item>
    < item>
    < attribute name =labeltranslatable =yes>退出< / attribute>
    < attribute name =action> app.quit< / attribute>
    < attribute name =accel>& lt; Primary& gt; q< / attribute>
    < / item>
    < / section>
    < / menu>
    < menu id =menubar>
    < submenu>
    < attribute name =label> _Help< / attribute>
    <节>
    < item>
    < attribute name =label> _About< / attribute>
    < attribute name =action> app.about< / attribute>
    < / item>
    < / section>
    < / submenu>
    < / menu>
    < / interface>
    '''

    class App:
    def __init __(self):
    self.app = Gtk.Application.new('org.liulang.test', 0)
    self.app.connect('startup',self.on_app_startup)
    self.app.connect('activate',self.on_app_activate)
    self.app.connect('shutdown ',self.on_app_shutdown)

    def run(self,argv):
    self.app.run(argv)
    $ b $ def on_app_startup(self,app):
    self.window = Gtk.ApplicationWindow.new(app)
    self.window.set_default_size(640,480)
    self.window.set_title('Gio Actions Demo')
    self.window.set_border_width(5)
    #无需连接delete-event / destroy信号

    app.add_window(self.window)

    label = Gtk .Label('Hello,Gtk3')
    self.window.add(标签)
    label.props.halign = Gtk.Align.CENTER
    label.props.valign = Gtk.Align.CENTER

    builder = Gtk.Builder()
    #最好从一个单独的文件加载ui
    builder.add_from_string(menus_str )
    builder.connect_signals(self)
    appmenu = builder.get_object('appmenu')
    app.set_app_menu(appmenu)
    menubar = builder.get_object('menubar')
    app.set_menubar(menubar)

    self.add_simple_action('preferences',
    self.on_action_preferences_activated)
    self.add_simple_action('about',self.on_action_about_activated)
    self.add_simple_action('quit',self.on_action_quit_activated)

    def on_app_activate(self,app):
    self.window.show_all()

    def on_app_shutdown(self,app):
    #在这里做一些清理工作,比如倾卸配置。
    pass
    $ b $ def on_action_preferences_activated(self,action,user_data):
    print('会弹出偏好设置对话框)

    on on_action_about_activated(self,action ,user_data):
    print('会显示关于对话框)

    def on_action_quit_activated(self,action,user_data):
    #这将关闭默认的gtk mainloop
    self.app.quit()

    def add_simple_action(self,name,callback):
    action = Gio.SimpleAction.new(name,None)
    action.connect 'activate',callback)
    self.app.add_action(action)

    $ b $ if if __name__ =='__main__':
    app = App()
    app.run(sys.argv)


    I'm trying to convert the menubar in my Gtk app so it will use GActions(from the Gio) as opposed of GtkActions in Python3 using GObject Instrospection.

    I've been trying to figure it out on my own but so far it seems awfully complicated and I didn't have much luck with it.

    If someone could please post an example of how to create a simple menu GAction based with

    • A submenu
    • A menu item with a stock ID icon / hotkey
    • A menu item with a non-stock icon / hotkey
    • A checked menu item
    • And radio menu item group
    • A disabled(grayed out) menu item

    It would really help me a lot.

    EDIT: This is the menubar I have in my window now:

    If someone could replicate the menu items shown using GioActions it would so I could figure out how they work it would be great.

    By the way, all of the actions I have use window callbacks not app callbacks, so this is a window menubar not a app menubar.

    解决方案

    A menubar is added now.

    #!/usr/bin/env python3
    
    # Copyright (C) 2013 LiuLang <gsushzhsosgsu@gmail.com>
    
    # Use of this source code is governed by GPLv3 license that can be found
    # in http://www.gnu.org/licenses/gpl-3.0.html
    
    from gi.repository import Gio
    from gi.repository import Gtk
    import sys
    
    menus_str ='''
    <?xml version="1.0"?>
    <interface>
      <menu id="appmenu">
        <section>
          <item>
            <attribute name="label" translatable="yes">Preferences</attribute>
            <attribute name="action">app.preferences</attribute>
          </item>
        </section>
        <section>
          <item>
            <attribute name="label" translatable="yes">About</attribute>
            <attribute name="action">app.about</attribute>
          </item>
          <item>
            <attribute name="label" translatable="yes">Quit</attribute>
            <attribute name="action">app.quit</attribute>
            <attribute name="accel">&lt;Primary&gt;q</attribute>
          </item>
        </section>
      </menu>
      <menu id="menubar">
        <submenu>
          <attribute name="label">_Help</attribute>
          <section>
            <item>
                  <attribute name="label">_About</attribute>
                  <attribute name="action">app.about</attribute>
                </item>
              </section>
            </submenu>
          </menu>
        </interface>
        '''
    
        class App:
            def __init__(self):
                self.app = Gtk.Application.new('org.liulang.test', 0)
                self.app.connect('startup', self.on_app_startup)
                self.app.connect('activate', self.on_app_activate)
                self.app.connect('shutdown', self.on_app_shutdown)
    
            def run(self, argv):
                self.app.run(argv)
    
            def on_app_startup(self, app):
                self.window = Gtk.ApplicationWindow.new(app)
                self.window.set_default_size(640, 480)
                self.window.set_title('Gio Actions Demo')
                self.window.set_border_width(5)
                # no need to connect delete-event/destroy signal
    
                app.add_window(self.window)
    
                label = Gtk.Label('Hello, Gtk3')
                self.window.add(label)
                label.props.halign = Gtk.Align.CENTER
                label.props.valign = Gtk.Align.CENTER
    
                builder = Gtk.Builder()
                # It is better to load ui from a seperate file
                builder.add_from_string(menus_str)
                builder.connect_signals(self)
                appmenu = builder.get_object('appmenu')
                app.set_app_menu(appmenu)
                menubar = builder.get_object('menubar')
                app.set_menubar(menubar)
    
                self.add_simple_action('preferences', 
                        self.on_action_preferences_activated)
                self.add_simple_action('about', self.on_action_about_activated)
                self.add_simple_action('quit', self.on_action_quit_activated)
    
            def on_app_activate(self, app):
                self.window.show_all()
    
            def on_app_shutdown(self, app):
                # do some cleaning job here, like dumping configuration.
                pass
    
            def on_action_preferences_activated(self, action, user_data):
                print('will popup preferences dialog')
    
            def on_action_about_activated(self, action, user_data):
                print('will show about dialog')
    
            def on_action_quit_activated(self, action, user_data):
                # This will close the default gtk mainloop
                self.app.quit()
    
            def add_simple_action(self, name, callback):
                action = Gio.SimpleAction.new(name, None)
                action.connect('activate', callback)
                self.app.add_action(action)
    
    
        if __name__ == '__main__':
            app = App()
            app.run(sys.argv)
    

    这篇关于如何在PyGI GTK中使用GIO Actions创建一个完整的菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆