Got some help last week in regard to a problem which put me on the trail
of understanding threading. Through reading and playing around I've this
working example. What I don't understand are how certain parts of it
work. BTW, I'm working on a windows PC.
import threading
import random, time
import gtk
#Initializing the gtk's thread engine
gtk.gdk.threads_init()
class FractionSetter(threading.Thread):
"""This class sets the fraction of the progressbar"""
def run(self):
progressbar.set_fraction(random.random())
print 'first'
time.sleep(2)
progressbar.set_fraction(random.random())
print 'second'
time.sleep(2)
gtk.main_quit()
def stop(self):
"""Stop method, sets the event to terminate the thread's
main loop"""
self.stopthread.set()
def main_quit(obj):
fs.stop()
gtk.main_quit()
#Gui bootstrap: window and progressbar
window = gtk.Window()
progressbar = gtk.ProgressBar()
window.add(progressbar)
window.show_all()
window.connect('destroy', main_quit)
#Creating and starting the thread
fs = FractionSetter()
fs.start()
gtk.gdk.threads_enter()
gtk.main()
gtk.gdk.threads_leave()
My question is:
a) What does putting gtk.main() between gtk.gdk.threads _enter &
gtk.gdk.threads _leave actually do?
b) Am I right in thinking the time.sleep(2) are there so that the
progressbar is changed and the gui window is updated?
c) Without the time.sleep(2) I'm thinking the progressbar would be
changed but the gui window wouldn't be updated. Correct?
Still getting my head around threads. See all sorts of references all of
which seem a wee bit beyond my comprehension at the moment. Any comments
would be appreciated.
Thanks
Brian
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/