A simple example of pigt and filaments

Can anyone give me a simple example related to threads in this way please.

The problem with my code is that when I click the One button, the GUI hangs before it completes. I want the buttons to remain responsive when the def is executed. How can I fix this?

class fun:
        wTree = None
        def __init__( self ):                
                self.wTree = gtk.glade.XML( "ui.glade" )

                dic = {
                        "on_buttonOne" : self.one,
                        "on_buttonTwo" : self.two,
                }
                self.wTree.signal_autoconnect( dic )              
                gtk.main()

        def sone(self, widget):
           time.sleep(1)
           print "1"
           time.sleep(1)
           print "2"
           time.sleep(1)
           print "3"
        def stwo(self, widget):
           time.sleep(1)
           print "4"
           time.sleep(1)
           print "5"
           time.sleep(1)
           print "6"
do=fun()

      

Enough, please help me.

+2


source to share


2 answers


When using gtk, it will start the main loop and you schedule whatever you want to do as events in the gtk loop. You don't need threads to do anything.

Here is a complete, complete, ready-to-run example that it uses glib.timeout_add

to accomplish what you want.



Note that pressing both buttons (or multiple times on a button) does not delay the gui and everything happens "at the same time" ...

import gtk
import glib

def yieldsleep(func):
    def start(*args, **kwds):
        iterable = func(*args, **kwds)
        def step(*args, **kwds):
            try:
                time = next(iterable)
                glib.timeout_add_seconds(time, step)
            except StopIteration:
                pass
        glib.idle_add(step)
    return start

class Fun(object):
    def __init__(self):
        window = gtk.Window()

        vbox = gtk.VBox()

        btnone = gtk.Button('one')
        btnone.connect('clicked', self.click_one)
        btnone.show()
        vbox.pack_start(btnone)

        btntwo = gtk.Button('two')
        btntwo.connect('clicked', self.click_two)
        btntwo.show()
        vbox.pack_start(btntwo)

        vbox.show()
        window.add(vbox)
        window.show()

    @yieldsleep
    def click_one(self, widget, data=None):
        yield 1 #time.sleep(1)
        print '1'
        yield 1 #time.sleep(1)
        print '2'
        yield 1 #time.sleep(1)
        print '3'

    @yieldsleep
    def click_two(self, widget, data=None):
        yield 1 #time.sleep(1)
        print '4'
        yield 1 #time.sleep(1)
        print '5'
        yield 1 #time.sleep(1)
        print '6'

do = Fun()
gtk.main()

      

+1


source


Using Python Threads: http://docs.python.org/library/threading.html

Sort of:

class SoneThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.start() # invoke the run method

    def run(self):
       time.sleep(1)
       print "1"
       time.sleep(1)
       print "2"
       time.sleep(1)
       print "3"

      



Now in sone, just call SoneThread (), which should work.

Also you need to call gtk.gdk.threads_init () to get python threads working with your GTK + application.

See: http://library.gnome.org/devel/pygtk/stable/gdk-functions.html#function-gdk--threads-init

+4


source







All Articles