0

I am using Gtk3 together with the threading module, because the program is receiving messages from another program in unknown intervals. Updating the GUI is done upon receiving a message from the other program, but for some reason not everything updates: Gtk.window.set_title works fine, while Gtk.Box.pack_end does not update inside the thread.

Here a simplified version of my current code (I removed communication details, since it works correctly):

#! /usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, Gdk, GObject

import time
from threading import Thread

class AppWindow(Gtk.ApplicationWindow):

    def __init__(self):
        Gtk.Window.__init__(self)
        self.grid = Gtk.Grid()
        self.add(self.grid)
        self.loop_box = Gtk.Box()
        self.loop_box.set_vexpand(True)
        self.grid.attach(self.loop_box, 0, 0, 1, 1)
        new_info = Gtk.Label('lollerino1')
        self.loop_box.pack_end(new_info, True, True, 0) # shown on GUI 

        self.communication_thread = Thread(target=self.manageIncomingCommunication)
        self.communication_thread.setDaemon(True)
        self.communication_thread.start()

    def manageIncomingCommunication(self):
        while True:
            if (msg['type'] == "WORK!"):
                GObject.idle_add(self.doGuiStuff, msg, priority=GObject.PRIORITY_DEFAULT)

            time.sleep(1)

    def doGuiStuff(self, msg_info):
        Gtk.Window.set_title(self, 'I update correctly') # updates
        new_info = Gtk.Label('lollerino2')
        self.loop_box.pack_end(new_info, True, True, 0) # does not update GUI
        # if I print here, printing is done correctly
        Gtk.Window.resize(self, 500, 500) # also updates

app = AppWindow()
GObject.threads_init() # tried with and without
app.connect('destroy', Gtk.main_quit)
app.show_all()
Gtk.main()

I also tried to manually add gtk main loop iterations while there are pending events, but nothing changed.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Shelyo
  • 3
  • 1

0 Answers0