【发布时间】:2021-09-12 21:34:11
【问题描述】:
我想让应用程序在后台运行,即使它的所有窗口都已关闭。有什么功能或东西可以做到这一点吗?我在 SO 上找不到答案。这是我的代码:
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)
当我关闭窗口时,程序停止。即使所有窗口都关闭了,如何让它继续运行?
【问题讨论】: