|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import Gio
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="GMenu Example", application=app)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
# start the application
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create a menu
|
|
Packit |
1470ea |
menu = Gio.Menu()
|
|
Packit |
1470ea |
# append to the menu three options
|
|
Packit |
1470ea |
menu.append("New", "app.new")
|
|
Packit |
1470ea |
menu.append("About", "app.about")
|
|
Packit |
1470ea |
menu.append("Quit", "app.quit")
|
|
Packit |
1470ea |
# set the menu as menu of the application
|
|
Packit |
1470ea |
self.set_app_menu(menu)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create an action for the option "new" of the menu
|
|
Packit |
1470ea |
new_action = Gio.SimpleAction.new("new", None)
|
|
Packit |
1470ea |
# connect it to the callback function new_cb
|
|
Packit |
1470ea |
new_action.connect("activate", self.new_cb)
|
|
Packit |
1470ea |
# add the action to the application
|
|
Packit |
1470ea |
self.add_action(new_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# option "about"
|
|
Packit |
1470ea |
about_action = Gio.SimpleAction.new("about", None)
|
|
Packit |
1470ea |
about_action.connect("activate", self.about_cb)
|
|
Packit |
1470ea |
self.add_action(about_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# option "quit"
|
|
Packit |
1470ea |
quit_action = Gio.SimpleAction.new("quit", None)
|
|
Packit |
1470ea |
quit_action.connect("activate", self.quit_cb)
|
|
Packit |
1470ea |
self.add_action(quit_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for "new"
|
|
Packit |
1470ea |
def new_cb(self, action, parameter):
|
|
Packit |
1470ea |
print("This does nothing. It is only a demonstration.")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for "about"
|
|
Packit |
1470ea |
def about_cb(self, action, parameter):
|
|
Packit |
1470ea |
print("No AboutDialog for you. This is only a demonstration.")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for "quit"
|
|
Packit |
1470ea |
def quit_cb(self, action, parameter):
|
|
Packit |
1470ea |
print("You have quit.")
|
|
Packit |
1470ea |
self.quit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|