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",
        self.window = None

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

    def do_startup(self):

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


    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"])

        return 0

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

    def on_quit(self, action, param):

if __name__ == "__main__":
    app = Application()

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()

    # ------Start the main GTK loop-------

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.