GtkApplication and GtkApplicationWindow from UI file

I’m trying to use GtkApplication with GtkApplicationWindow. The latter is defined in a UI file. I can’t figure out how to properly set things up.

This is my Glade UI template outline:

And this is the Python code so far:

import sys

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gio, Gtk

# NOTE: Not being used for this attempt.
class AppWindow(Gtk.ApplicationWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


class Application(Gtk.Application):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, application_id="org.opensource.mynotes",
                         flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
                         **kwargs)
        self.window = None

        self.add_main_option("test", ord("t"), GLib.OptionFlags.NONE,
                             GLib.OptionArg.NONE, "Command line test", None)

    def do_startup(self):
        Gtk.Application.do_startup(self)


    def do_activate(self):
        if not self.window:
            # self.window = AppWindow(application=self, title="My Notes")
            builder = Gtk.Builder()
            builder.add_from_file("./mynotes.ui")
            app_window = builder.get_object("app_window")
            app_window.show_all()
            self.window = app_window

        self.window.present()

    def do_command_line(self, command_line):
        options = command_line.get_options_dict()
        # convert GVariantDict -> GVariant -> dict
        options = options.end().unpack()

        if "test" in options:
            # This is printed on the main instance
            print("Test argument recieved: %s" % options["test"])

        self.activate()
        return 0

    def on_about(self, action, param):
        about_dialog = Gtk.AboutDialog(transient_for=self.window, modal=True)
        about_dialog.present()

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

if __name__ == "__main__":
    app = Application()
    app.run(sys.argv)

How to properly use GtkApplicationWindow from Glade instead of from Python code? In other words, how to start the Application making use of an UI file which already contains GtkApplicationWindow? My current attempt causes the application open and immediately close. No errors on the terminal.

Hmm. Looks like adding app_window.set_application(self) in do_activate works. Still, eager to know of any other considerations.

I think I had a problem with that too some years ago, and google gave me a solution from stackoverflow. I think I manually edited the xml file. At least the xml from

worked. It starts with

  <object id="window" class="GtkApplicationWindow">
    <property name="visible">True</property>

I think the visible property was important too, without it I had to use window.showAll() in code.

Maybe I got it from this link:

1 Like

You should never set a toplevel window to visible in the UI file. That will immediately show it, but you want to programmatically control when that happens.

Well – I think for plain example code or toy programs it may be better to set toplevel window to visible in the UI file. Otherwise users do not use showAll() and wonder why they don’t see the GUI. I got at least one issue about this:

Or users may ask: Why have I to call window.showAll() – my virtual basic app does not need that…

You are gonna run into problems if a GtkApplicationWindow does not know about its application by the time it’s shown.

OK, then I will fix that in the Nim examples, thanks.

It looks like your code is missing Gtk.main(), which starts the main loop for GTK.

Try adding it and see if it helps.
For example:

if __name__ == "__main__":
    app = Application()
    app.run(sys.argv)

    # ------Start the main GTK loop-------
    Gtk.main()

Our code is fine, there is no gtk_main() needed when we use GtkApplication. See

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.