Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

python - Simple pygtk and threads example please

Can someone give me a simple example involving threads in this manner, please.

Problem with my code is that when I click button One, GUI freezes until its finished. I want buttons to stay responsive when def is being executed. How can i fix that?

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()

Pretty please, help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Something like:

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(), that should work.

Also you need to call gtk.gdk.threads_init() in order to make python threads work with your GTK+ application.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...