Faire un don ! | | style | statistiques | contactez-nous | plan | lettre d'information

Retourner aux forums || Retourner au forum Programmation.python

Programmation.python : pygtk et les threads

Posté par Moonz () le 06 août 2005
Bonjour tout le monde
J'ai un problème plutôt étrange, regardez le code source suivant


import sys, gtk, threading

def test(*args):
dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Plop")
dialog.run()
dialog.destroy()

def test_thread():
gtk.gdk.threads_enter()
test()
gtk.gdk.threads_leave()

def test_threaded(*args):
threading.Thread(target=test_thread).start()

gtk.gdk.threads_init()

w = gtk.Window(gtk.WINDOW_TOPLEVEL)
b1 = gtk.Button("gtk.Dialog dans le thread principal")
b2 = gtk.Button("gtk.Dialog dans un autre thread")
h = gtk.HButtonBox()

b1.connect("clicked", test)
b2.connect("clicked", test_threaded)

w.add(h)
h.pack_start(b1)
h.pack_start(b2)

w.show_all()

gtk.main()


Lorsque l'on créé un gtk.Dialog dans le thread principal, tout fonctionne correctement
Ensuite, lors d'un premier clic sur "gtk.Dialog dans un autre thread", ça fonctionne correctement. Mais si je reclique dessus, le programme gèle :-/
Une idée d'où peut venir ce problème ?
Merci d'avance :)

> Lire le message (3 commentaires, moyenne: 1,3).  

Cette discussion est archivée, il n'est plus possible de laisser des commentaires.

Note : les commentaires appartiennent à ceux qui les ont postés. Nous n'en sommes pas responsables.

RE

Posté par Sylvain (Jabber id, page perso, ) le 06/08/2005 à 15:47. (lien). Évalué à 1.

Bonjour ami pythonien personnellement ayant un peu galerer sur les thread avec les meme problemes que tu expose (gel des fenetres). En faite j'ai appris a utiliser les thread de maniere a ne plus avoir de probleme, non sans mal. J'ai tendance a faire comme ceci, qui a mon avis doit pas etre loin de la route a suivre :


class Thread(threading.Thread):
    def __init__(self):
         ###Initialisation de notre thread
         self.stop=False

    def setStop(self):
        """Trip the stop flag."""
        self.stop = True
        
    def run(self):
        while not self.stop :
             ###CODE DU THREAD#########
             ##On peut mettre un le flag de stop en fin de code si on veut pas un thread infini###
             self.setStop()
             return()

class gui(wx.App):
    def OnInit(self):
      ###Classe init Wxwindows ou GTK#####

    def thread(self):
        ###On appelle notre thread########
        variable = Thread()


En esperant t'avoir guidé :)
A+ bon courage

  • [^]Désoler jai oublier un truc

    Posté par Sylvain (Jabber id, page perso, ) le 06/08/2005 à 15:51. (lien). Évalué à 0.

    
    class Thread(threading.Thread):
        def __init__(self):
             ###Initialisation de notre thread
             self.stop=False
    
        def setStop(self):
            """Trip the stop flag."""
            self.stop = True
            
        def run(self):
            while not self.stop :
                 ###CODE DU THREAD#########
                 ##On peut mettre un le flag de stop en fin de code si on veut pas un thread infini###
                 self.setStop()
            return()
    
    class gui(wx.App):
        def OnInit(self):
          ###Classe init Wxwindows ou GTK#####
    
        def thread(self):
            ###On appelle notre thread########
            variable = Thread()
            variable.start() ###Oublie dans le post avant
    
    
    
    :)

    • [^]Re: Désoler jai oublier un truc

      Posté par Moonz () le 06/08/2005 à 16:36. (lien). Évalué à 1.

      Ca marche pas beaucoup mieux


      import sys, gtk, threading

      def test(*args):
      dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Plop")
      dialog.run()
      dialog.destroy()

      class Thread(threading.Thread):
      def __init__(self):
      threading.Thread.__init__(self)

      def run(self):
      gtk.gdk.threads_enter()
      test()
      gtk.gdk.threads_leave()

      return

      class Main(gtk.Window):
      def __init__(self):
      gtk.gdk.threads_init()

      gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
      b1 = gtk.Button("gtk.Dialog dans le thread principal")
      b2 = gtk.Button("gtk.Dialog dans un autre thread")
      h = gtk.HButtonBox()

      b1.connect("clicked", test)
      b2.connect("clicked", self.thread)

      h.pack_start(b1)
      h.pack_start(b2)

      self.add(h)
      self.show_all()

      gtk.main()

      def thread(self, btn):
      t = Thread()
      t.start()

      Main()



      Le plus étrange, c'est que ça fonctionne la première fois, mais pas la deuxième.

      Merci quand même :)

Revenir en haut de page || Retourner aux forums || Retourner au forum Programmation.python