如何使用 python 中的 GTK3 检查 window 是否存在并可能更新其中的数据?

How to check with GTK3 in python that the window exists and possibly update the data in it?

我有一个 class 调用一个 window 显示内容 (Gtk.ApplicationWindow),我想检查 window 是否存在。如果它存在,它更新数据,如果不存在,它创建一个新的 window 的新实例。如何检查 window 是否存在?

编辑:

我的代码:

class LogListener:

    def __init__(self):
        print('do sth')
        AlertView()

class AlertView(Gtk.ApplicationWindow):

    def __init__(self):
        super().__init__()

        try:
            if self.window:
                self.label2.set_label('String 2')
        except AttributeError:
            self.builder = Gtk.Builder()
            self.builder.add_from_file("resources//alert_view.glade")
            self.builder.connect_signals(self)
            self.window = self.builder.get_object("window1")
            self.window.set_border_width(10)
            self.label2 = self.builder.get_object('label2')
            self.label2.set_label('String 1')
            self.button1 = self.builder.get_object('button1')
            self.button1.connect('clicked', self.on_button_clicked)
            self.window.set_keep_above(True)
            self.window.show_all()
            self.window.connect("destroy", Gtk.main_quit)

    def on_button_clicked(self, button):
        print('click')
        self.window.connect("destroy", Gtk.main_quit)

class ReportTray(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(
            application_id="exampleapp.com"       )

    def do_activate(self):
        if not hasattr(self, "my_app_settings"):
            self.hold()
            scheduler = BackgroundScheduler()
            trigger = interval.IntervalTrigger(seconds=5)
            scheduler.add_job(LogListener, trigger, max_instances=1)
            scheduler.start()
        else:
            print("Already running!")

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

if __name__ == "__main__":
    app = ReportTray()
    app.run()
'''

不能运行下面的代码,所以可能是错误的!

class LogListener:
    def __init__(self):
        print('do sth')
        app.alert_view.set_label("TEST")

class AlertView(Gtk.ApplicationWindow):
    def __init__(self):
        super().__init__()
        self.builder = Gtk.Builder()
        self.builder.add_from_file("resources//alert_view.glade")
        ...

    def set_label(self, text):
        # if state withdraw: deiconify
        self.label2.set_label(text)


class ReportTray(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(
            application_id="exampleapp.com"       )
            self.alert_view. = AlertView()

if __name__ == "__main__":
    app = ReportTray()
    app.run()

@stovfl 我修改了您的解决方案,并且有效!现在我可以修改标签并检查 window 实例是否存在。谢谢!


class LogListener:

    def __init__(self):
        print('do sth')
        if app.alert_view:
            app.alert_view.set_label("TEST")
        else:
            app.alert_view = AlertView()
            app.alert_view.set_label("TEST2")

class AlertView(Gtk.ApplicationWindow):
    def __init__(self):
        super().__init__()
        self.builder = Gtk.Builder()
        self.builder.add_from_file("resources//alert_view.glade")
        ...

    def set_label(self, text):
        # if state withdraw: deiconify
        self.label2.set_label(text)
        self.window.show_all()

    def close(self, *args):
        self.window.destroy()
        app.alert_view = None

class ReportTray(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(
            application_id="exampleapp.com"
        )       
        self.alert_view = None