Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Created July 18, 2022 14:33
Show Gist options
  • Save AnthoniG/0e3ea2cf049cb167a99d661bb6d93c69 to your computer and use it in GitHub Desktop.
Save AnthoniG/0e3ea2cf049cb167a99d661bb6d93c69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Python imports
import sys
# GTK imports
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gio
from gi.repository import Gtk
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_border_width(10)
self.set_default_size(640, 480)
open_selection = Gtk.ModelButton(action_name="open_file", label="Open")
about_selection = Gtk.ModelButton(action_name="about_application", label="About")
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, margin=10, spacing=10)
vbox.add(open_selection)
vbox.add(about_selection)
vbox.show_all()
self.popover = Gtk.Popover()
self.popover.add(vbox)
self.popover.set_position(Gtk.PositionType.BOTTOM)
menu_button = Gtk.MenuButton(popover=self.popover)
menu_icon = Gtk.Image.new_from_icon_name("open-menu-symbolic", Gtk.IconSize.MENU)
menu_icon.show()
menu_button.add(menu_icon)
menu_button.show()
headerbar = Gtk.HeaderBar()
headerbar.props.show_close_button = True
headerbar.props.title = "Hamburger Menu Demo"
headerbar.add(menu_button)
headerbar.show()
self.set_titlebar(headerbar)
def open_file(self, widget):
print("Open file")
class Application(Gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id="org.example.myapp")
self.window = None
def do_startup(self):
Gtk.Application.do_startup(self)
action = Gio.SimpleAction.new("open_file", None)
action.connect("activate", self.open_file)
action.set_enabled(True)
self.add_action(action)
action = Gio.SimpleAction.new("about_application", None)
action.connect("activate", self.on_about)
self.add_action(action)
action = Gio.SimpleAction.new("quit", None)
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 open_file(self, action, param):
print("Open file")
def on_about(self, action, param):
print("About application")
def on_quit(self, action, param):
self.quit()
if __name__ == "__main__":
app = Application()
app.run(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment