Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
t.start()

    def calc(self,treeiter):
        store=self.builder.get_object('liststore1')
        per=0
        while 1:
            print "Calcing and changing percent,per="+str(per)
            tore.set_value(treeiter,4,str(int(per))+"%")
            per+=1
            time.sleep(1)

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?

share|improve this question
    
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

2 Answers 2

up vote 2 down vote accepted

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):
        store=self.builder.get_object('liststore1')
        print "Calcing and changing percent,per="+str(self.per)
        tore.set_value(treeiter,4,str(int(self.per))+"%")
        self.per+=1
        return True #important if you want to keep the timer running
share|improve this answer
    
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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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