File: popover_menu.py

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (76 lines) | stat: -rw-r--r-- 2,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gi

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

# This would typically be its own file
MENU_XML = """
<?xml version='1.0' encoding='UTF-8'?>
<interface>
  <menu id='app-menu'>
    <section>
        <item>
            <attribute name='label'>About</attribute>
            <attribute name='action'>app.about</attribute>
        </item>
        <item>
            <attribute name='label'>Quit</attribute>
            <attribute name='action'>app.quit</attribute>
        </item>
    </section>
  </menu>
</interface>
"""


class AppWindow(Gtk.ApplicationWindow):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.set_default_size(300, 200)

        headerbar = Gtk.HeaderBar()
        self.set_titlebar(headerbar)

        builder = Gtk.Builder.new_from_string(MENU_XML, -1)
        menu_model = builder.get_object("app-menu")

        button = Gtk.MenuButton(menu_model=menu_model)
        headerbar.pack_end(button)


class Application(Gtk.Application):
    def __init__(self, **kwargs):
        super().__init__(application_id="com.example.App", **kwargs)
        self.window = None

    def do_startup(self):
        Gtk.Application.do_startup(self)

        action = Gio.SimpleAction(name="about")
        action.connect("activate", self.on_about)
        self.add_action(action)

        action = Gio.SimpleAction(name="quit")
        action.connect("activate", self.on_quit)
        self.add_action(action)

    def do_activate(self):
        # We only allow a single window and raise any existing ones
        if not self.window:
            # Windows are associated with the application
            # when the last one is closed the application shuts down
            self.window = AppWindow(application=self, title="Main Window")

        self.window.present()

    def on_about(self, action, param):
        about_dialog = Gtk.AboutDialog(transient_for=self.window, modal=True)
        about_dialog.present()

    def on_quit(self, action, param):
        self.quit()


app = Application()
app.run(None)