使用 GIO Actions 在 PyGI GTK 中创建完整菜单

73 阅读2分钟

在 GTK 应用程序中,菜单栏是一个常见的 UI 元素,用于组织和提供应用程序的功能。传统上,GTK 应用程序使用 GtkActions 来创建菜单。然而,随着 GIO 库的引入,GIO Actions 成为创建菜单的推荐方法。GIO Actions 提供了一个更灵活、更强大且更现代的方式来处理应用程序的动作和菜单。

  1. 解决方案

使用 GIO Actions 创建菜单涉及以下步骤:

  1. 创建一个 Gio.Menu 对象,它将作为菜单的基础。
  2. 向菜单添加子菜单和菜单项。
  3. 为每个菜单项关联一个 Gio.SimpleAction 对象。
  4. 将菜单添加到应用程序的 AppMenu 或 Menubar 中。

以下是一个使用 GIO Actions 在 PyGI GTK 中创建完整菜单的代码示例:

#!/usr/bin/env python3

# Import the necessary libraries
from gi.repository import Gio
from gi.repository import Gtk
import sys

# Define the menu structure
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>
    '''

# Define the main application class
class App:
    def __init__(self):
        # Create the application object
        self.app = Gtk.Application.new('org.liulang.test', 0)

        # Connect to the startup, activate, and shutdown signals
        self.app.connect('startup', self.on_app_startup)
        self.app.connect('activate', self.on_app_activate)
        self.app.connect('shutdown', self.on_app_shutdown)

    # Run the application
    def run(self, argv):
        self.app.run(argv)

    # Handle the app startup signal
    def on_app_startup(self, app):
        # Create the main window
        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)

        # Add the window to the application
        app.add_window(self.window)

        # Create a label to display text in the window
        label = Gtk.Label('Hello, Gtk3')
        self.window.add(label)
        label.props.halign = Gtk.Align.CENTER
        label.props.valign = Gtk.Align.CENTER

        # Create a builder to load the menu UI from a string
        builder = Gtk.Builder()
        builder.add_from_string(menus_str)

        # Connect signals to the builder objects
        builder.connect_signals(self)

        # Get the AppMenu and Menubar objects from the builder
        appmenu = builder.get_object('appmenu')
        menubar = builder.get_object('menubar')

        # Set the AppMenu and Menubar to the application
        app.set_app_menu(appmenu)
        app.set_menubar(menubar)

        # Create simple actions for the menu items
        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)

    # Handle the app activate signal
    def on_app_activate(self, app):
        # Show the main window
        self.window.show_all()

    # Handle the app shutdown signal
    def on_app_shutdown(self, app):
        # Do any necessary cleanup before exiting the application
        pass

    # Define the callback for the Preferences menu item
    def on_action_preferences_activated(self, action, user_data):
        print('Preferences menu item activated')

    # Define the callback for the About menu item
    def on_action_about_activated(self, action, user_data):
        print('About menu item activated')

    # Define the callback for the Quit menu item
    def on_action_quit_activated(self, action, user_data):
        # Quit the application
        self.app.quit()

    # Add a simple action to the application
    def add_simple_action(self, name, callback):
        action = Gio.SimpleAction.new(name, None)
        action.connect('activate', callback)
        self.app.add_action(action)


# Create an instance of the main application class and run the application
if __name__ == '__main__':
    app = App()
    app.run(sys.argv)

这个代码示例演示了如何使用 GIO Actions 创建一个完整的菜单,包括子菜单、菜单项、图标和热键。它还展示了如何连接动作到回调函数,以便在用户选择菜单项时执行相应的功能。

我希望这个代码示例对你有帮助。如果你有任何其他的问题,请随时向我提问。