I am developing a multithreaded application in pygtk using quickly and stuck with threads. So I am experimenting with various possibilities and found out that my thread work only when I do something in the gui Here is my code

t = threading.Thread(target=self.calc,args=(treeiter))
t.daemon = True

    def calc(self,treeiter):
        while 1:
            print "Calcing and changing percent,per="+str(per)

I am trying to update the value in a liststore by thread but it only get update when I click some button or some other gui events why is that so? why is the thread not running in the background?

  • Enthu appanu undakunnathu? :) – Tachyons Mar 31 '13 at 10:08
  • pygtk padikkan vendi axel oru gui undakamennu karuthi – Sreevisakh Mar 31 '13 at 10:13
  • 1
    Try stackoverflow – Tachyons Mar 31 '13 at 14:00

Using time.sleep() is not a good idea when you are using gtk. You could try use a timer event. (I don't use quickly anymore but this should work i think.)

from gi.repository import GLib

class Just_for_correct_coding():
    self.per = int()

    def start(self):
        GLib.timeout_add_seconds(1, self.calc)

    def calc(self,treeiter):
        print "Calcing and changing percent,per="+str(self.per)
        return True #important if you want to keep the timer running
  • AttributeError: 'gi.repository.Gtk' object has no attribute 'timeout_add – Sreevisakh Mar 31 '13 at 13:05
  • 1
    Seems the gi.repository.Gtk libery don't has that functions included (with import gtk does). I have changed the code you can try this ;). – Thomas15v Mar 31 '13 at 17:34
  • yes it worked GLib.timeou_add – Sreevisakh Mar 31 '13 at 17:35

Threads and GTK don't always play nice together. There are a few tricks that help, but don't be surprised to have weird bugs. I recently removed all threads from an application and it feels much better.

So, the first thing you should think about is whether you can rewrite your application to take advantage of the GLib async methods.

If you insist on having threads, keep these two rules in mind:

  1. Call GLib.treads_init() as early as possible in your code
  2. When calling GUI events in a thread, wrap them in a GLib.idle_add()

These are rules, not guidelines. You must always do this when working with threads.

  • so I need to wrap the first three lines in idle_add? are they same as GObject.idle_add? – Sreevisakh Mar 31 '13 at 10:06
  • it worked with the help of Glib.timeout_add thank you – Sreevisakh Mar 31 '13 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.