Created
August 17, 2022 22:43
-
-
Save nate-xyz/cd6dc5a7ae7ce27490f6ca850568ae7a to your computer and use it in GitHub Desktop.
gtk4 libadwaita python hello_world
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import gi | |
| gi.require_version('Gtk', '4.0') | |
| gi.require_version('Adw', '1') | |
| from gi.repository import Gtk, Gio, Adw | |
| class Window(Gtk.ApplicationWindow): | |
| __gtype_name__ = 'HelloWorldWindow' | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| self.set_default_size(600, 300) | |
| self.set_title("hello_world") | |
| self.build_ui() | |
| def build_ui(self): | |
| self.header = Gtk.HeaderBar() | |
| self.set_titlebar(self.header) | |
| self.menu_button = Gtk.MenuButton() | |
| menu = self.build_menu() | |
| self.menu_button.set_menu_model(menu) | |
| self.menu_button.set_icon_name("open-menu-symbolic") | |
| self.header.pack_end(self.menu_button) | |
| hello = Gtk.Label() | |
| hello.set_label('Hello, World!') | |
| self.set_child(hello) | |
| def build_menu(self): | |
| menu = Gio.Menu.new() | |
| menu.append("_Preferences", "app.preferences") | |
| menu.append("_Keyboard Shortcuts", "win.show-help-overlay") | |
| menu.append("_About hello_world", "app.about") | |
| return menu | |
| class AboutDialog(Gtk.AboutDialog): | |
| def __init__(self, parent): | |
| Gtk.AboutDialog.__init__(self) | |
| self.props.program_name = 'hello_world' | |
| self.props.version = "0.1.0" | |
| self.props.authors = ['Author'] | |
| self.props.copyright = '2022 Author' | |
| self.props.logo_icon_name = 'org.hello.World' | |
| self.props.modal = True | |
| self.set_transient_for(parent) | |
| class Application(Adw.Application): | |
| """The main application singleton class.""" | |
| def __init__(self): | |
| super().__init__(application_id='org.hello.World', | |
| flags=Gio.ApplicationFlags.FLAGS_NONE) | |
| self.create_action('quit', self.quit, ['<primary>q']) | |
| self.create_action('about', self.on_about_action) | |
| self.create_action('preferences', self.on_preferences_action) | |
| def do_activate(self): | |
| """Called when the application is activated. | |
| We raise the application's main window, creating it if | |
| necessary. | |
| """ | |
| win = self.props.active_window | |
| if not win: | |
| win = Window(application=self) | |
| win.present() | |
| def on_about_action(self, widget, _): | |
| """Callback for the app.about action.""" | |
| about = AboutDialog(self.props.active_window) | |
| about.present() | |
| def on_preferences_action(self, widget, _): | |
| """Callback for the app.preferences action.""" | |
| print('app.preferences action activated') | |
| def create_action(self, name, callback, shortcuts=None): | |
| """Add an application action. | |
| Args: | |
| name: the name of the action | |
| callback: the function to be called when the action is | |
| activated | |
| shortcuts: an optional list of accelerators | |
| """ | |
| action = Gio.SimpleAction.new(name, None) | |
| action.connect("activate", callback) | |
| self.add_action(action) | |
| if shortcuts: | |
| self.set_accels_for_action(f"app.{name}", shortcuts) | |
| def main(): | |
| """The application's entry point.""" | |
| app = Application() | |
| return app.run(sys.argv) | |
| if __name__ == '__main__': | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment