简体   繁体   English

如何使用Gtk.Builder使用菜单栏创建PyGObject应用程序?

[英]How to create PyGObject application with a menubar using Gtk.Builder?

There is no full documentation about how to use Gtk.Builder in PyGObject to create a menubar. 有一个关于如何使用不完整的文档Gtk.BuilderPyGObject创建一个菜单栏。

I don't use that Gtk.UIManager because it is deprecated. 我不使用该Gtk.UIManager因为它已被弃用。 The example code below is based on my experience with Gtk.UIManager . 以下示例代码基于我对Gtk.UIManager经验。

In the example should appear a menubar with Foo as a top menu group having an clickable item Bar . 在示例中应显示一个菜单栏,其中Foo作为具有可单击项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'>
            <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()

The current error is 当前错误是

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

I am unsure about 我不确定

  • How to create the XML string. 如何创建XML字符串。
  • How to get the menubar-widget. 如何获取菜单栏小部件。
  • How to create Actions/Click-handlers for menu items. 如何为菜单项创建操作/点击处理程序。

Of course the question could be extended to toolbars but I wouldn't made it to complexe. 当然,这个问题可以扩展到工具栏,但我不会使其复杂化。

btw: I don't want to use Gtk.Application.set_menubar() . 顺便说一句:我不想使用Gtk.Application.set_menubar() Because there is no Gtk.Application.set_toolbar() and currently I see no advantage on having a Gtk-based application object. 因为没有Gtk.Application.set_toolbar()而且目前我看不到基于Gtk的应用程序对象的优势。

EDIT : I also tried this variant (without any success): 编辑 :我也尝试了此变体(没有任何成功):

gio_menu = builder.get_object('TheMenu')
menubar = Gtk.Menubar.new_from_model(gio_menu)

My answer is based on a foreign answer on the gtk-dev-app mailinglist . 我的答案基于gtk-dev-app mailinglist上的外国答案

I prefere Variant 3. 我更喜欢变体3。

Variant 1: with XML-String 变体1:带有XML-String

Please be aware of the different naming of the action between the XML-string ( win.bar ) and the Gio.SimpleAction ( bar ). 请注意,XML字符串( win.bar )和Gio.SimpleActionbar )之间的动作命名不同。

#!/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()

Variant 2: without XML but with Actions 变体2:没有XML但有操作

I prefere this variant because it doesn't use (human unreadable XML) and Gtk.Builder . 我更喜欢这种变体,因为它不使用(人类不可读的XML)和Gtk.Builder Here you create the structure of your menu as a data structure based on Gio.Menu and connect a Action (which itself is connected to an event handler) to it's items. 在这里,您将菜单结构创建为基于Gio.Menu的数据结构,并将Action (其本身已连接到事件处理程序)连接到其项目。 Out of that informations the widget for the menubar is kind of generated. 根据这些信息,会生成菜单栏的小部件。

#!/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()

Variant 3: Old-school, easy without XML, Actions or Gio layer 方案3:老式,无需XML,Actions或Gio层即可轻松实现

This variant works kind of "old school" because you simply build your menu widgets together and connect signalls directly to them. 这种变体有点像“老派”,因为您只需将菜单小部件一起构建并将信号直接连接到它们。 This works without using a underlying and abstract data structure (eg Gio.MenuModel or an XML-string) and without a Application class. 这可以在不使用基础和抽象数据结构(例如Gio.MenuModel或XML字符串)且没有Application类的情况下工作。

#!/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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM