URL: https://linuxfr.org/forums/programmation-python/posts/pygtk-et-les-threads--2 Title: PyGTK et les threads. Authors: thor_tue Date: 2010年09月01日T18:26:04+02:00 Tags: Score: 0 Bonjour. J'essaye de faire la synthèse de ce que j'ai glané sur la toile à propos de pygtk et les threads. J'essaye donc un programme très simple d'expérimentation. J'ai une classe principale, une qui gère 2 threads et une classe thread proprement dite. Mon problème : lorsque les threads 1 et 2 sont lancés simultanément, j'ai toujours un seul des deux labels qui est mis à jour (analogie avec une situation XOR). Je commence à bloquer, je n'ai pas trouvé de documentation vraiment approfondie sur ce sujet. D'avance merci. #!/usr/bin/env python # -*- coding: utf-8 -*- from threads_manager import ThreadsManager import gobject gobject.threads_init() # <----- import gtk class Main: £spaces£ £/spaces£def __init__(self): £spaces£ £/spaces£self.window = gtk.Window() £spaces£ £/spaces£self.button1 = gtk.Button('Start/stop thread 1') £spaces£ £/spaces£self.button2 = gtk.Button('Start/stop thread 2') £spaces£ £/spaces£self.labels = [] £spaces£ £/spaces£self.labels.append(gtk.Label('*')) £spaces£ £/spaces£self.labels.append(gtk.Label('*')) £spaces£ £/spaces£self.table = gtk.Table() £spaces£ £/spaces£self.table.attach(self.labels[0], 0, 1, 0, 1, xpadding=10) £spaces£ £/spaces£self.table.attach(self.button1, 1, 2, 0, 1) £spaces£ £/spaces£self.table.attach(self.labels[1], 0, 1, 1, 2, xpadding=10) £spaces£ £/spaces£self.table.attach(self.button2, 1, 2, 1, 2) £spaces£ £/spaces£self.window.add(self.table) £spaces£ £/spaces£self.threads_manager = ThreadsManager(self) £spaces£ £/spaces£self.button1.connect('clicked', self.threads_manager.switch_thread_state, 1) £spaces£ £/spaces£self.button2.connect('clicked', self.threads_manager.switch_thread_state, 2) £spaces£ £/spaces£self.window.connect('destroy', self.quit) £spaces£ £/spaces£self.window.set_position(gtk.WIN_POS_CENTER) £spaces£ £/spaces£self.window.show_all() £spaces£ £/spaces£def main(self): £spaces£ £/spaces£gtk.main() £spaces£ £/spaces£return 0 £spaces£ £/spaces£def quit(self, widget_window): £spaces£ £/spaces£# À finir. Informer les threads qu'ils doivent stopper afin de £spaces£ £/spaces£# quitter proprement ? £spaces£ £/spaces£gtk.main_quit() if __name__ == '__main__': £spaces£ £/spaces£main = Main() £spaces£ £/spaces£main.main() #!/usr/bin/env python # -*- coding: utf-8 -*- import threading from my_thread import MyThread class ThreadsManager: £spaces£ £/spaces£def __init__(self, main): £spaces£ £/spaces£self.main = main £spaces£ £/spaces£self.th1 = MyThread(self.main, 1) £spaces£ £/spaces£self.th2 = MyThread(self.main, 2) £spaces£ £/spaces£def switch_thread_state(self, widget_button, nb): £spaces£ £/spaces£if nb == 1: £spaces£ £/spaces£if self.th1.is_running == True: £spaces£ £/spaces£self.th1.stop() £spaces£ £/spaces£else: £spaces£ £/spaces£self.th1.start() £spaces£ £/spaces£else: # Therefore nb == 2 £spaces£ £/spaces£if self.th2.is_running == True: £spaces£ £/spaces£self.th2.stop() £spaces£ £/spaces£else: £spaces£ £/spaces£self.th2.start() #!/usr/bin/env python # -*- coding: utf-8 -*- import threading, time import gobject import gtk class MyThread(threading.Thread): £spaces£ £/spaces£def __init__(self, main, nb): £spaces£ £/spaces£threading.Thread.__init__(self) £spaces£ £/spaces£self.main = main £spaces£ £/spaces£self.nb = nb £spaces£ £/spaces£self.is_running = False £spaces£ £/spaces£def start(self): £spaces£ £/spaces£print 'start thread', self.nb £spaces£ £/spaces£self.is_running = True £spaces£ £/spaces£while self.is_running == True: £spaces£ £/spaces£gobject.idle_add(self.update_label) # <----- £spaces£ £/spaces£time.sleep(0.1) £spaces£ £/spaces£gtk.main_iteration() # <----- £spaces£ £/spaces£def stop(self): £spaces£ £/spaces£print 'stop thread', self.nb £spaces£ £/spaces£self.is_running = False £spaces£ £/spaces£def update_label(self): £spaces£ £/spaces£# Le but de cette fonction est de montrer comment modifier £spaces£ £/spaces£# un objet gtk dans la boucle principale depuis un thread. £spaces£ £/spaces£self.main.labels[self.nb-1].set_text(str(time.time()))

AltStyle によって変換されたページ (->オリジナル) /