Wednesday 29 June 2022

Run action after Gtk window is shown

I have a multi-window Gtk application, which is an installer. During the installation process, which takes some time, I want to show a Window with a label to notify the user that the installation is in progress. So I tried to bind the respective method to the show event.

However, that causes the appearance of the window to be delayed until the the method finishes, after which the next window is immediately shown. The result is, that the previous window shows, then the screen goes blank for the duration of the actual installation and then the final window is shown.

I boiled the issue down to the fact, that the show event is obviously triggered, before the window is actually shown.

Here's a minimal snipped to clarify my issue. The window shows after the call to sleep(), not before.

#! /usr/bin/env python3

from time import sleep

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


class GUI(Gtk.ApplicationWindow):

    def __init__(self):
        """Initializes the GUI."""
        super().__init__(title='Gtk Window')
        self.set_position(Gtk.WindowPosition.CENTER)
        self.grid = Gtk.Grid()
        self.add(self.grid)
        self.label = Gtk.Label()
        self.label.set_text('Doing stuff')
        self.grid.attach(self.label, 0, 0, 1, 1)
        self.connect('show', self.on_show)

    def on_show(self, *args):
        print('Doing stuff.')
        sleep(3)
        print('Done stuff.')


def main() -> None:
    """Starts the GUI."""

    win = GUI()
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

How can I achieve, that the window shows before the method on_show() is called?

The desired program flow is

  1. Show window
  2. run installation
  3. hide window (and show next one)

without any user interaction.



from Run action after Gtk window is shown

No comments:

Post a Comment