<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python3

import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Gio", "2.0")
from gi.repository import Gtk, Gio


class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Popover Menu Example")
        self.set_default_size(300, 200)

        # Create a Gio.Menu model
        menu_model = Gio.Menu()
        menu_model.append("New", "app.new")
        menu_model.append("Open", "app.open")
        menu_model.append("Save", "app.save")
        menu_model.append_section(None, Gio.MenuSection.new_from_model(Gio.Menu()))
        menu_model.append("Quit", "app.quit")

        # Create a GtkPopoverMenuBar using the menu model
        popover_menu_bar = Gtk.PopoverMenuBar()
        popover_menu_bar.set_menu_model(menu_model)

        # Create a button to trigger the popover
        menu_button = Gtk.Button.new_from_icon_name("open-menu-symbolic", Gtk.IconSize.MENU)
        menu_button.connect("clicked", self.on_menu_button_clicked, popover_menu_bar)

        # Create a HeaderBar and add the button and popover to it
        header_bar = Gtk.HeaderBar()
        header_bar.set_show_close_button(True)
        header_bar.pack_end(menu_button)
        self.set_titlebar(header_bar)

        # Create an Application and add actions
        app = Gtk.Application.get_default()
        app.add_action(Gio.SimpleAction.new("new", None), self.on_new_action)
        app.add_action(Gio.SimpleAction.new("open", None), self.on_open_action)
        app.add_action(Gio.SimpleAction.new("save", None), self.on_save_action)
        app.add_action(Gio.SimpleAction.new("quit", None), self.on_quit_action)
        app.set_accels_for_action("app.quit", ["&lt;Control&gt;q"])

        # Add a label to the window
        label = Gtk.Label(label="Content Area")
        self.add(label)
        self.show_all()

    def on_menu_button_clicked(self, button, popover_menu_bar):
        # Position the popover relative to the button
        popover_menu_bar.popup()

    def on_new_action(self, action, param):
        print("New action")

    def on_open_action(self, action, param):
        print("Open action")

    def on_save_action(self, action, param):
        print("Save action")

    def on_quit_action(self, action, param):
        Gtk.main_quit()


# Create and run the application
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
Gtk.main()
</pre></body></html>