Search code examples
pythonmultithreadinggtk3glibglade

Python : How to deal with threads priority in Gtk3


I am building an user interface using Python, Gtk3 and Glade. I want to change several things on the UI at the same time (i.e start an animation and display a new text) which leads to the application freezing.

I have read that Gtk wasn't thread safe so I didn't used the Thread module. Instead, I used Glib.idle_add and Gdk.threads_add_idle functions. I am tryig to update a treeview, display some text and show an animated logo at the same time. The application works but it freezes a few seconds and then everything appears at the same time. I try to set different priorities to the threads but it does'nt seem to fix it.

Gtk.threads_add_idle(Glib.PRIORITY_DEFAULT, label.set_text, "text_to_set")
Gtk.threads_add_igle(GLib.PRIORITY_DEFAULT, function_to_display_logo)

I expect the different texts and the treeview and the logo to be displayed without any freeze. Does anyone know how I can fix that ?


Solution

  • I found out what my error was. I was using the GLib.idle_add function too many times even in some cases where I had no use for it.

    For example in the main code I had :

    Glib.idle_add(my_function,buffer)

    but my_function looked like this :

    def myfuntion(buffer):
        GLib.idle_add(buffer.set_text,"text")
    

    I deleted the GLib.idle_add call in the main code and now it works perfectly.