Hello!! I'm really new to python and pygtk and Glade, I'm sorry I'm still on the dark side using Windows 2000 :-(
What I'm trying to do is a simple Testing Program Simulator, that means that I will press a button and then I'm supposed to show a cross or a checkmark after simulating a test. Actually there are 4 tests.

So what I tried to do is to use a thread to run some for loops and time.sleep(1) for some seconds inside each for loop.

Then a textbox changes and an image appears.

The problem is that the program SOMETIMES works as it should, but some other times the GUI freezes and the images don't get loaded.

Sometimes the program seems to keep on running while the GUI freezes, some other times the program execution seems to freeze and the GUI freezes. Sometimes if I press ctrl + C in the console the program seems to go out of some hidden loop and then the GUI unfreezes.

I'm really frustrated, I already read like 5 examples for using threads and I can't find my mistake. Could somebody help me please???

Here's my code:

import pygtk
import gtk
import gtk.glade
import time
import threading

bandera1 = 0
 
class Sequence(threading.Thread):#subclass Thread
	def __init__(self):#make it possible to pass the time in seconds that we want the timer to run
		
		threading.Thread.__init__(self)#call the Thread's constructor
		
	def run(self):#define what we want our Timer thread to do
		
		for i in range(0,4):
			time.sleep(1)#time.sleep(self.runTime)#have it sleep for runTime seconds
		print '0'

		GUI.TarjetaProbada.set_from_file('C:\Documents and 

Settings\practicantes\Escritorio\PracticasPython\GUIs\GUISimple\pixmaps\CPU.JPG')
		GUI.TextoPrueba1.set_markup('<big><big><big>Prueba de voltajes del Microcontrolador</big></big></big>')
		
		for i in range(0,4):
			time.sleep(1)
		GUI.ImagenPrueba1.set_from_file('C:\Documents and 

Settings\practicantes\Escritorio\PracticasPython\GUIs\GUISimple\pixmaps\check mark_Green.bmp')
		print '1'
		
		GUI.TextoPrueba2.set_markup('<big><big><big>Prueba de voltajes del Regulador</big></big></big>')
		for i in range(0,4):
			time.sleep(1)
		GUI.ImagenPrueba2.set_from_file('C:\Documents and 

Settings\practicantes\Escritorio\PracticasPython\GUIs\GUISimple\pixmaps\check mark_Green.bmp')
		print '2'
		
		GUI.TextoPrueba3.set_markup('<big><big><big>Prueba de Polaridad en Diodo</big></big></big>')
		for i in range(0,4):
			time.sleep(1)
		GUI.ImagenPrueba3.set_from_file('C:\Documents and 

Settings\practicantes\Escritorio\PracticasPython\GUIs\GUISimple\pixmaps\cross mark_RED.bmp')
		print '3'
		
		
		GUI.TextoPrueba4.set_markup('<big><big><big>Prueba de funcionamiento del IrDA</big></big></big>')
		for i in range(0,4):
			time.sleep(1)
		GUI.ImagenPrueba4.set_from_file('C:\Documents and 

Settings\practicantes\Escritorio\PracticasPython\GUIs\GUISimple\pixmaps\check mark_Green.bmp')
		print '4'

		
def botonazo(self):

	GUI.ExplicacionPrueba.set_markup('<big><big><big><big><big><big>Iniciando Prueba del 

CPU</big></big></big></big></big></big>')
	
	for i in range(0,4):
	
		t = Sequence()
		
		global bandera1
		print 'bandera1 = ' + str(bandera1)
		#t.run()
		t.start()
		#t.join()
	



class GUIPruebasAutomatizadas:
	def __init__(self):
		
		self.GUI = gtk.glade.XML('guiejecucionpruebas.glade')
		self.VentanaPrincipal = self.GUI.get_widget('VentanaGUIPruebasAutomatizadas')
		
		
		self.vbox5 = self.GUI.get_widget('vbox5')
		
		self.TarjetaProbada = self.GUI.get_widget('ImagenProbando')
	
		self.Boton = self.GUI.get_widget('BotonIniciarPrueba') #this is a button widget in the glade GUI
		self.Boton.connect('clicked',botonazo)
		
		self.ExplicacionPrueba = self.GUI.get_widget('ExplicacionPrueba')
		#self.ExplicacionPrueba.connect()
		
		self.TextoPrueba1 = self.GUI.get_widget('TextoPrueba1') #This are all textboxes widgets in the glade GUI
		self.TextoPrueba2 = self.GUI.get_widget('TextoPrueba2')
		self.TextoPrueba3 = self.GUI.get_widget('TextoPrueba3')
		self.TextoPrueba4 = self.GUI.get_widget('TextoPrueba4')
		
		self.ImagenPrueba1 = self.GUI.get_widget('ImagenPrueba1') #this are Images widgets in the glade GUI
		self.ImagenPrueba2 = self.GUI.get_widget('ImagenPrueba2')
		self.ImagenPrueba3 = self.GUI.get_widget('ImagenPrueba3')
		self.ImagenPrueba4 = self.GUI.get_widget('ImagenPrueba4')
		

gtk.gdk.threads_init()


GUI=GUIPruebasAutomatizadas()

print 'contando!!!!'

gtk.threads_enter()
gtk.main()
gtk.threads_leave()

Dani AI

Generated

The freeze is almost certainly caused by updating GTK widgets from non‑GUI threads and by starting multiple identical worker threads at once. GTK (PyGTK) requires that all widget operations run on the main loop thread; calling widget methods (like set_from_file or set_markup) directly from a background Thread leads to race conditions and intermittent deadlocks. In 's code the for loop inside botonazo starts the same Sequence four times, which amplifies the problem.

The simplest, robust approach is: keep the long work in a background thread, but schedule any GUI changes to run on the main loop using gobject.idle_add. That guarantees the modification executes in the GUI thread. Example pattern:

import gobject

def _set_image(widget, path):
    widget.set_from_file(path)
    return False   # run once

def _set_markup(widget, markup):
    widget.set_markup(markup)
    return False

# inside worker thread:
gobject.idle_add(_set_markup, GUI.TextoPrueba1, "<big>Prueba...</big>")
gobject.idle_add(_set_image, GUI.ImagenPrueba1, r"C:\path\checkmark.bmp")

Practical fixes tied to the thread above:

  • Replace the loop that starts four identical Sequence() threads with a single thread that drives all four tests sequentially, or pass an index/parameter to each thread so they operate on separate widgets without overlap.
  • Do not call t.run() (that blocks the main thread); use t.start() and never t.join() from the button handler.
  • Use safe file paths (raw strings r"..." or forward slashes) to avoid accidental escape sequences, and pre-load images (pixbuf) in the GUI thread if disk I/O is a concern.
  • Wrap run() body in try/except and log exceptions; uncaught thread exceptions can be silent and confusing.

is correct that tooling and platform quirks exist, but switching toolkits is not required here: correct thread-to-GUI handoff (via gobject.idle_add or properly guarded threads_enter/threads_leave) will make the app stable on Windows.

My experience with pyGTK, I can not get it to work with Windows XP. The Linux folks have no problems. Before you go completely nuts with an unstable product, I would recommend switching to the wxPython GUI.

Just my opinion!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.