[pygtk] Win32: Problems with gtk.threads_init()

Cedric Gustin cedric.gustin at swing.be
Thu Aug 7 11:02:18 WST 2003


On Mon, 2003-08-04 at 18:29, Maik Hertha wrote:

> I have an application that uses threads. As the call to gtk.threads_init() is wrapped with try: except:, there where no problems. As win32 for py-gtk is build with threadings on by default, the application start seems to hang on gtk.threads_init().
> 
> - --- code:
> 	try:
> 		gtk.threads_init()
> 	except RuntimeError:
> 		print 'keine threads!'
> 		HAS_GTK_THREADS = False
> 	app = AppWindow()
> 	app.run()
> - --- code /
> 
> If I comment out the gtk.threads_init call the application starts as excepted. Otherwise the application is build, but the gtk.mainloop seems not to work properly. Because I have no compiler environment installed, I can not provide stack-traces or so.
> Environment:
> WinXP SP1, Python 2.3, pygtk-1.99.17-2.3, gtk-runtime 2.2.1.2 (dropline).

Make sure that you use threads_enter/leave(). Here is an example of a
very simple multithreaded application that works well on my machine
(same config as yours).

Cedric

-------------------------

#!/usr/bin/env python

""" Simple pyGTK multithreading example"""

# Adapted from a post by Alif Wahid on the pyGTK mailinglist.
# Cedric Gustin, August 2003

import sys
import time
import gtk

from threading import Thread

threadcount=0

class Test (Thread):
    def __init__ (self,button, count=0):
        Thread.__init__(self)
        self.count = count
        self.button=button

    def run (self):
        for i in range(0,10):
            time.sleep(1)
            # Acquire and release the lock each time.
            gtk.threads_enter()
            self.button.set_label("Thread %002d - %d" % (self.count,i))
            gtk.threads_leave()
        gtk.threads_enter()
        self.button.set_label("  Start Thread  ")
        gtk.threads_leave()

def start_new_thread (button, data=None):
    global threadcount
    threadcount += 1
    a = Test(button,threadcount)
    a.start()


def hello(*args):
    """ Callback function that is attached to the button """
    print "Hello World"
    window.destroy()

def destroy(*args):
    """ Callback function that is activated when the program is destoyed
"""
    window.hide()
    gtk.main_quit()

# Initialize threads
gtk.threads_init()

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", destroy)
window.set_border_width(10)

button = gtk.Button("  Start Thread  ")
button.connect("clicked", start_new_thread,button)
window.add(button)
button.show()

window.show_all()
gtk.threads_enter()
gtk.main()
gtk.threads_leave()

-------------------------



More information about the pygtk mailing list