IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

Gestion d'une IHM Tkinter par un thread


Sujet :

Tkinter Python

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Gestion d'une IHM Tkinter par un thread
    Bonjour,

    Je voudrais gérer mon IHM par un thread (pour éviter de bloquer toute mon application avec la mainloop() ).
    J'ai essayé quelque chose mais le thread ne veut pas se lancer, voici mon code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import Tkinter
    import threading
     
    class GUIThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.window=GUIInterface()
     
     
        def run(self):
            self.window.mainloop()
     
     
        def stop(self):
            self.window.destroy()
     
     
    class GUIInterface(Tkinter.Tk):
        def __init__(self):
            Tkinter.Tk.__init__(self)
            self.title("Monitor")
     
     
            self.labelText=Tkinter.Label(self,text="MonText")
            self.labelText.pack()
     
            self.buttonExit=Tkinter.Button(self, text="Exit",command=self.exit)
            self.buttonExit.pack()
     
     
        def exit(self):
            self.destroy()
     
     
    newGUIThread=GUIThread()
    newGUIThread.start()
    Si je fais simplement newGUIThread.run() au lieu de newGUIThread.start() ma fenêtre s'affiche bien mais bien sur je n'utilise pas de thread donc mon programme reste bloqué
    Python me répond:
    RuntimeError: Calling Tcl from different appartment

    Si vous aviez une idée...

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut la solution
    Finalement j'ai trouvé la réponse! Si jamais ça peut servir à quelqu'un:
    Je ne sais pas pourquoi mais si je mets mon self.window=GUIInterface() dans le __init__ ça ne marche pas mais si je le mets dans le run juste avant de lancer la mainloop ça fonctionne très bien...
    Voici donc le code corrigé:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import Tkinter
    import threading
     
    class GUIThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            #ici ça ne marchera pas self.window=GUIInterface()
     
     
        def run(self):
            #là ça marche
            self.window=GUIInterface()
     
     
            self.window.mainloop()
     
     
     
     
        def stop(self):
            self.window.destroy()
     
     
    class GUIInterface(Tkinter.Tk):
        def __init__(self):
            Tkinter.Tk.__init__(self)
            self.title("Monitor")
     
     
            self.labelText=Tkinter.Label(self,text="MonText")
            self.labelText.pack()
     
            self.buttonExit=Tkinter.Button(self, text="Exit",command=self.exit)
            self.buttonExit.pack()
     
     
        def exit(self):
            self.destroy()
     
     
    newGUIThread=GUIThread()
    newGUIThread.start()

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Gestion de l'état des widgets Tkinter par des Threads
    Par piokml dans le forum Général Python
    Réponses: 3
    Dernier message: 18/10/2012, 11h00
  2. Gestion d'une IHM Qt depuis un thread POSIX
    Par lulafitt dans le forum Multithreading
    Réponses: 1
    Dernier message: 02/03/2011, 00h28
  3. Réponses: 0
    Dernier message: 01/08/2008, 10h57
  4. Comment modifier une BufferedImage alimenté par un Thread
    Par jlassiramzy dans le forum AWT/Swing
    Réponses: 3
    Dernier message: 02/08/2006, 15h04
  5. [SWT]mise a jour ihm SWT par un thread
    Par will82 dans le forum SWT/JFace
    Réponses: 1
    Dernier message: 06/08/2004, 11h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo