S
S
Sly_tom_cat .2017-12-15 17:19:31
GTK+
Sly_tom_cat ., 2017-12-15 17:19:31

GTK: need to open a dialog for receiving data from an external program (via stdout) how?

The essence of the problem is that I made a separate thread for reading the stdout of an external program, but creating a window from this thread knocks GTK into crusts, because in GTK everything must be processed by the main thread (which runs in GTK.main()).
As I understand it, I need to somehow push the event so that the main thread, processing it, draws the desired dialog. Well, or from the main thread, you need to somehow find out that data has appeared in the stdout of an external program and they need to be displayed in a dialog. It would be possible to check stdout by timer, but I would like some kind of "interactive" ....
I understand that I'm stupid, but with GTK I'm "on you" ...
If anything - I do it all on the third python and on gtk3 from gi .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sly_tom_cat ., 2017-12-16
@Sly_tom_cat

In short, I found it.
Actually the main loop is provided by GLib. Actually, I already worked with him when I used timers.
It turned out that the timer is not the only source that can initiate callbacks from the main loop.
There is also such a thing as stream watchers, more specifically lazka.github.io/pgi-docs/GLib-2.0/functions.html#G...
I.e. we set which stream and for which condition to monitor (I needed for the event of data availability for reading - this is GLib.IOCondition.IN), as well as the callback function and the data that should be passed to it in the parameters.
The default priority is 0.
Nuances:
1. The channel is passed to the callback as the first parameter, the condition that was caught as the second, and only the data as the third (if they were specified when creating the watcher).
2. The callback function should return True if you want to continue monitoring, if you return False / None, then the watcher is removed from the event sources for the main loop.
Here's what I got:

...
    def stderr_reader(pipe, _):
      data = pipe.readline()
      if data != "":
        logger.debug(data[:-1])
      return True

    proc = Popen([external_prog, cfgFile],
                   bufsize=1,
                   universal_newlines=True,
                   stdin=PIPE, stderr=PIPE)

    io_add_watch(proc.stderr, 0, IOCondition.IN, stderr_reader)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question