Start/Stop Thread et pygtk !
Bjr,
Bon, encore un problème de thread...
C'est franchemenet pas évident les thread avec gtk ...
Pour l'exemple, je veux juste un bouton qui lance un thread,et un autre qui le stop.
La façon de lancer le thread, qui fonctionne en python, je l'ai trouvé sur le forum (qui date de 2006 qd même :))
Pour l'appliquer en gtk, je vois pas !
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #! /usr/bin/env python
# -*- coding:Utf*8 *-*-
import gtk
import time, threading
class ComWatcher(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = False
def run(self):
"""Le code que le thread devra exécuter."""
self.running = True
while self.running:
time.sleep(1)
print 'ok'
def stop(self):
self.running = False
class Gui:
def __init__(self):
self.fenetre = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.fenetre.connect("destroy", lambda wid: gtk.main_quit())
boite_v = gtk.VBox(True, 2)
self.fenetre.add(boite_v)
bouton = gtk.Button("bouton start")
bouton.connect("clicked", self.salut, "bouton start")
bouton.show()
boite_v.pack_start(bouton, True, True, 2)
bouton = gtk.Button("bouton bye")
bouton.connect("clicked", self.bye, "bouton bye")
bouton.show()
boite_v.pack_start(bouton, True, True, 2)
boite_v.show()
self.fenetre.show()
self.watcher = ComWatcher()
def salut(self,widget,event):
self.watcher.start()
def bye(self,widget,event):
self.watcher.stop()
self.watcher.join()
def main(self):
gtk.main()
start=Gui()
start.main() |
Merci d'avance à celui qui me sortira de l'impasse ;)