Search code examples
pythongtk3pygtk

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


I have a class that calls up a window with content to display(Gtk.ApplicationWindow), I would like to check if the window exists. If it exists it updates the data, if not, it creates a new instance of a new window. How to check if a window exists?

Edit:

My code:

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

Solution

  • Couldn't run the below code, so may be erroneous!

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