summaryrefslogtreecommitdiffstats
path: root/python-gnome-app
blob: 61f9c7756183223cf93a8ca5fd2459f512b7ad66 (plain)
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
#!/usr/bin/python3

import sys
from gi.repository import Gio, Gtk


class PythonApp(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(
            self, application_id="org.example.PythonGnome")

        self.connect("activate", self.on_activate)
        self.connect("startup", self.on_startup)

    def on_startup(self, app):
        self.window = Gtk.ApplicationWindow(application=app)

        hello_world = Gio.SimpleAction(name="hello-world", parameter_type=None)
        self.add_action(hello_world)
        self.add_accelerator("<Primary>h", "app.hello-world", None)
        hello_world.connect("activate", self.on_hello_world)

        quit = Gio.SimpleAction(name="quit", parameter_type=None)
        self.add_action(quit)
        self.add_accelerator("<Primary>q", "app.quit", None)
        quit.connect("activate", self.on_quit)

        appmenu = Gio.Menu.new()
        section = Gio.Menu.new()
        hello_world_item = Gio.MenuItem.new("Hello world!", "app.hello-world")
        quit_item = Gio.MenuItem.new("Quit", "app.quit")
        appmenu.append_section(None, section)
        section.append_item(hello_world_item)
        section.append_item(quit_item)
        self.set_app_menu(appmenu)

        button = Gtk.Button(
            label="Hello world!", action_name="app.hello-world")
        self.window.add(button)

    def on_activate(self, app):
        self.window.show_all()

    def on_hello_world(self, action=None, param=None):
        print("Hello world!")

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

app = PythonApp()
exit_status = app.run(None)
sys.exit(exit_status)