代码之家  ›  专栏  ›  技术社区  ›  buhtz

如何使用Gtk创建带有菜单栏的PyGObject应用程序。建设者

  •  5
  • buhtz  · 技术社区  · 7 年前

    没有关于如何使用的完整文档 Gtk.Builder 在里面 PyGObject 创建菜单栏。

    我不用这个 Gtk.UIManager 因为它已被弃用。 下面的示例代码基于我的经验 Gtk。UIManager

    在示例中,应显示一个菜单栏 Foo公司 作为具有可单击项的顶部菜单组 酒吧

    #!/usr/bin/env python3
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    from gi.repository import Gio
    
    class Window(Gtk.ApplicationWindow):
        def __init__(self):
            Gtk.Window.__init__(self)
            self.set_default_size(200, 100)
    
            #
            self.interface_info = """
            <interface>
              <menu id='TheMenu'>
                <section>
                  <attribute name='foo'>Foo</attribute>
                  <item>
                    <attribute name='bar'>Bar</attribute>
                  </item>
                </section>
              </menu>
            </interface>
            """
    
            builder = Gtk.Builder.new_from_string(self.interface_info, -1)
    
            action_bar = Gio.SimpleAction.new('bar', None)
            action_bar.connect('activate', self.on_menu)
            self.add_action(action_bar)
    
            menubar = builder.get_object('TheMenu')
    
            # layout
            self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
            self.layout.pack_start(menubar, True, True, 0)
            self.add(self.layout)
    
            self.connect('destroy', Gtk.main_quit)
            self.show_all()
    
        def on_menu(self, widget):
            print(widget)
    
    if __name__ == '__main__':
        win = Window()
        Gtk.main()
    

    当前错误为

    Traceback (most recent call last):
      File "./_menubar.py", line 46, in <module>
        win = Window()
      File "./_menubar.py", line 36, in __init__
        self.layout.pack_start(menubar, True, True, 0)
    TypeError: argument child: Expected Gtk.Widget, but got gi.repository.Gio.Menu
    

    我不确定

    • 如何创建XML字符串。
    • 如何获取菜单栏小部件。
    • 如何为菜单项创建操作/单击处理程序。

    当然,这个问题可以扩展到工具栏,但我不会让它变得复杂。

    顺便说一句:我不想使用 Gtk.Application.set_menubar() .因为没有 Gtk.Application.set_toolbar() 目前,我认为使用基于Gtk的应用程序对象没有任何优势。

    编辑 :我还尝试了此变体(未成功):

    gio_menu = builder.get_object('TheMenu')
    menubar = Gtk.Menubar.new_from_model(gio_menu)
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   buhtz    7 年前

    我的答案是基于 foreign answer gtk开发应用程序邮件列表

    我更喜欢变体3。

    变量1:使用XML字符串

    请注意 行动 在XML字符串之间( win.bar )以及 Gio.SimpleAction ( bar )。

    #!/usr/bin/env python3
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    from gi.repository import Gio
    
    class Window(Gtk.ApplicationWindow):
        def __init__(self):
            Gtk.Window.__init__(self)
            self.set_default_size(200, 100)
    
            #
            self.interface_info = """
            <interface>
              <menu id='TheMenu'>
                <submenu>
                  <attribute name='label'>Foo</attribute>
                  <item>
                    <attribute name='label'>Bar</attribute>
                    <attribute name='action'>win.bar</attribute>
                  </item>
                </submenu>
              </menu>
            </interface>
            """
    
            builder = Gtk.Builder.new_from_string(self.interface_info, -1)
    
            action_bar = Gio.SimpleAction.new('bar', None)
            action_bar.connect('activate', self.on_menu)
            self.add_action(action_bar)
    
            menumodel = builder.get_object('TheMenu')
            menubar = Gtk.MenuBar.new_from_model(menumodel)
    
            # layout
            self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
            self.layout.pack_start(menubar, False, False, 0)
            self.add(self.layout)
    
            self.connect('destroy', Gtk.main_quit)
            self.show_all()
    
        def on_menu(self, action, value):
            print('Action: {}\nValue: {}'.format(action, value))
    
    if __name__ == '__main__':
        win = Window()
        Gtk.main()
    

    变体2:没有XML但有操作

    我更喜欢这种变体,因为它不使用(人类无法读取的XML)和 Gtk.Builder 。 在这里,您可以创建菜单的结构,作为基于 Gio.Menu 并连接 Action (它本身连接到事件处理程序)到它的项。从这些信息中可以生成菜单栏的小部件。

    #!/usr/bin/env python3
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    from gi.repository import Gio
    
    class Window(Gtk.ApplicationWindow):
        def __init__(self):
            Gtk.Window.__init__(self)
            self.set_default_size(200, 100)
    
            action_bar = Gio.SimpleAction.new('bar', None)
            action_bar.connect('activate', self.on_menu)
            self.add_action(action_bar)
    
            # root of the menu
            menu_model = Gio.Menu.new()
    
            # menu item "Bar"
            menu_item = Gio.MenuItem.new('Bar', 'win.bar')
    
            # sub-menu "Foo" with item "Bar"
            menu_foo = Gio.Menu.new()
            menu_foo.append_item(menu_item)
            menu_model.append_submenu('Foo', menu_foo)
    
            # create menubar widget from the model
            menubar = Gtk.MenuBar.new_from_model(menu_model)
    
            # layout
            self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
            self.layout.pack_start(menubar, False, False, 0)
            self.add(self.layout)
    
            self.connect('destroy', Gtk.main_quit)
            self.show_all()
    
        def on_menu(self, action, value):
            print('Action: {}\nValue: {}'.format(action, value))
    
    if __name__ == '__main__':
        win = Window()
        Gtk.main()
    

    变体3:老派,无需XML、操作或Gio层即可轻松完成

    这种变体有点“老派”,因为您只需将菜单小部件构建在一起,并将信号直接连接到它们。这不需要使用底层和抽象的数据结构(例如。 Gio.MenuModel 或XML字符串),并且没有应用程序类。

    #!/usr/bin/env python3
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    class Window(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self)
            self.set_default_size(200, 100)
    
            # create menubar
            menubar = self._create_menubar()
    
            # create a toolbar
            toolbar = self._create_toolbar()
    
            # layout
            self.layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
            self.layout.pack_start(menubar, False, False, 0)
            self.layout.pack_start(toolbar, False, False, 0)
            self.add(self.layout)
    
            self.connect('destroy', Gtk.main_quit)
            self.show_all()
    
        def _create_menubar(self):
            # menu item 'Bar'
            item_bar = Gtk.MenuItem.new_with_label('Bar')
            item_bar.connect('activate', self.on_menu)
    
            # sub menu for 'Bar'
            menu_foo = Gtk.Menu.new()
            menu_foo.append(item_bar)
    
            # main menu 'Foo' with attached sub menu
            item_foo = Gtk.MenuItem.new_with_label('Foo')
            item_foo.set_submenu(menu_foo)
    
            # the menubar itself
            menubar = Gtk.MenuBar.new()
            menubar.append(item_foo)
    
            return menubar
    
        def _create_toolbar(self):
            toolbar = Gtk.Toolbar.new()
    
            # button with label
            bar_item = Gtk.ToolButton.new(None, 'Bar')
            bar_item.connect('clicked', self.on_menu)
            toolbar.insert(bar_item, -1)
    
            # button with icon
            bar_item = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OK)
            bar_item.connect('clicked', self.on_menu)
            toolbar.insert(bar_item, -1)
    
            return toolbar
    
        def on_menu(self, caller):
            print(caller)
    
    if __name__ == '__main__':
        win = Window()
        Gtk.main()