Python - How do I keep a Gtk.Application running when there are no windows?

I want to keep the application running in the background even when all it's windows are closed. Is there a function or something to do this? I couldn't find an answer on SO. Here is my code:

import gi
import sys

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class App(Gtk.Application):
    """The application manager."""

    def __init__(self, *args, **kwargs):
        Gtk.Application.__init__(
            self,
            *args,
            application_id="org.app_test",
            # flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
            **kwargs
        )

        # We are only allowed to have one window
        self.window = None

    def do_activate(self):
        """Activate the app."""

        # Create the window, if it does not exist
        if self.window is None:
            self.window = AppWindow(application=self, title="MusicApp", name="0")
            self.window.present()

    def do_startup(self):
        """Start the app."""
        Gtk.Application.do_startup(self)

class AppWindow(Gtk.ApplicationWindow):
    """The main window for the application."""

    def __init__(self, *args, application, **kwargs):
        Gtk.ApplicationWindow.__init__(
            self,
            *args,
            application=application,
            **kwargs
        )

        # The application
        self.app = application

        self.show_all()

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

When I close the window, the program stops. How do I make it continue running even if all the windows are closed?

Upvotes: 3

Views: 549

Answers (1)

Thomas
Thomas

Reputation: 68

You can use my_app.hold() to make the app think there is an extra window open and then will not close the app when the window is closed because it thinks there is another non-existent window open.

PyGObject API Reference

Upvotes: 4

Related Questions