Python - 我如何在没有 windows 的情况下保留 Gtk.Application 运行?
Python - How do I keep a Gtk.Application running when there are no windows?
我想将应用程序 运行 保留在后台,即使它的所有 windows 都已关闭。有没有功能或东西可以做到这一点?我找不到关于 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)
当我关闭 window 时,程序停止。即使所有 windows 都已关闭,我如何让它继续 运行?
您可以使用 my_app.hold()
让应用程序认为有一个额外的 window 打开,然后当 window 关闭时不会关闭应用程序,因为它认为还有另一个不存在 window 打开。
我想将应用程序 运行 保留在后台,即使它的所有 windows 都已关闭。有没有功能或东西可以做到这一点?我找不到关于 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)
当我关闭 window 时,程序停止。即使所有 windows 都已关闭,我如何让它继续 运行?
您可以使用 my_app.hold()
让应用程序认为有一个额外的 window 打开,然后当 window 关闭时不会关闭应用程序,因为它认为还有另一个不存在 window 打开。