|
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 |
# constructor for a window (the parent window) with a label
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="GMenu Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 200)
|
|
Packit |
1470ea |
label = Gtk.Label()
|
|
Packit |
1470ea |
label.set_text("This application goes boom!")
|
|
Packit |
1470ea |
self.add(label)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create the message_action (a Gio.SimpleAction) - for the window
|
|
Packit |
1470ea |
message_action = Gio.SimpleAction.new("message", None)
|
|
Packit |
1470ea |
# connect the signal from the action to the function message_cb()
|
|
Packit |
1470ea |
message_action.connect("activate", self.message_cb)
|
|
Packit |
1470ea |
# add the action to the application
|
|
Packit |
1470ea |
app.add_action(message_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for the signal "activate" from the message_action
|
|
Packit |
1470ea |
# in the menu of the parent window
|
|
Packit |
1470ea |
def message_cb(self, action, parameter):
|
|
Packit |
1470ea |
# a Gtk.MessageDialog
|
|
Packit |
1470ea |
messagedialog = Gtk.MessageDialog(parent=self,
|
|
Packit |
1470ea |
flags=Gtk.DialogFlags.MODAL,
|
|
Packit |
1470ea |
type=Gtk.MessageType.WARNING,
|
|
Packit |
1470ea |
buttons=Gtk.ButtonsType.OK_CANCEL,
|
|
Packit |
1470ea |
message_format="This action will cause the universe to stop existing.")
|
|
Packit |
1470ea |
# connect the response (of the button clicked) to the function
|
|
Packit |
1470ea |
# dialog_response()
|
|
Packit |
1470ea |
messagedialog.connect("response", self.dialog_response)
|
|
Packit |
1470ea |
# show the messagedialog
|
|
Packit |
1470ea |
messagedialog.show()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def dialog_response(self, widget, response_id):
|
|
Packit |
1470ea |
# if the button clicked gives response OK (-5)
|
|
Packit |
1470ea |
if response_id == Gtk.ResponseType.OK:
|
|
Packit |
1470ea |
print("*boom*")
|
|
Packit |
1470ea |
# if the button clicked gives response CANCEL (-6)
|
|
Packit |
1470ea |
elif response_id == Gtk.ResponseType.CANCEL:
|
|
Packit |
1470ea |
print("good choice")
|
|
Packit |
1470ea |
# if the messagedialog is destroyed (by pressing ESC)
|
|
Packit |
1470ea |
elif response_id == Gtk.ResponseType.DELETE_EVENT:
|
|
Packit |
1470ea |
print("dialog closed or cancelled")
|
|
Packit |
1470ea |
# finally, destroy the messagedialog
|
|
Packit |
1470ea |
widget.destroy()
|
|
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 quit_cb(self, action, parameter):
|
|
Packit |
1470ea |
self.quit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create a menu (a Gio.Menu)
|
|
Packit |
1470ea |
menu = Gio.Menu()
|
|
Packit |
1470ea |
# append a menu item with label "Message" and action "app.message"
|
|
Packit |
1470ea |
menu.append("Message", "app.message")
|
|
Packit |
1470ea |
# append a menu item with label "Quit" and action "app.quit"
|
|
Packit |
1470ea |
menu.append("Quit", "app.quit")
|
|
Packit |
1470ea |
# set menu as the menu for the application
|
|
Packit |
1470ea |
self.set_app_menu(menu)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a new simpleaction - for the application
|
|
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 |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|