Pages : 1
#1 Le 24/10/2016, à 12:42
- jmg17
Tkinter et threads parallèles
Bonjour,
Est-il possible d'afficher simultanément plusieurs fenêtres Tk à l'aide de threads ?
J'essaie d'afficher un message d'attente (ou un gif animé) pour un programme qui fait des calculs un peu longs, et qui affiche ensuite le résultat sous forme de courbes dans une nouvelle fenêtre Tk.
Le message d'attente seul s'affiche correctement, le résultat des calculs, seul, s'affiche correctement. Mais, dans des thread parallèles, ça ne fonctionne plus. De manière aléatoire, j'obtiens un message d'erreur « RuntimeError: Calling Tcl from different appartment » et l'affichage de la deuxième fenêtre est incomplet.
Il semble que tkinter n'arrive pas a gérer les 2 affichages en même temps.
Ci_dessous, résumé du programme avec juste l'affichage qui pose problème. (j'ai simulé le temps de calcul avec un « time.sleep() » dans le run du thread 2.)
Il y a sûrement une erreur quelque part, mais je ne trouve pas.
Merci pour votre aide.
JM
from tkinter import*
import threading
import time
def main():
t1= Thread1(attente )
t2= Thread2(graphique)
t1.start()
t2.start()
class Thread1(threading.Thread):
def __init__(self, attente ):
threading.Thread.__init__(self)
self.attente = attente
def run(self):
print("starting t1")
attente()
class Thread2(threading.Thread):
def __init__(self, graphique):
threading.Thread.__init__(self)
self.graphique = graphique
def run(self):
time.sleep(3)
print("starting t2")
graphique()
def graphique():
fen = Tk()
gra = Graphe(fen, 1000, 300)
gra.configure(bg="red")
gra.grid(row=1, column=1, columnspan=3)
vol= Volume(fen, 1000, 150)
vol.configure(bg="green")
vol.grid(row=2, column=1, columnspan=3)
combo = Combo(fen, 20, 20)
combo.configure(bg="yellow")
combo.grid(row=3, column=2, rowspan=2)
text1 = Label(fen,text="aaaaaaa", font="Arial 20")
text1.grid(row=3, column=1)
text2 = Label(fen, text="bbbbbbbb", font="Arial 20")
text2.grid(row=4, column=1)
text3=Label(fen,text="cccccccc", font="Arial 20")
text3.grid(row=3, column=3)
text4=Label( fen,text="ddddddddd", font="Arial 20")
text4.grid(row=4, column=3)
fen.mainloop()
class Volume(Canvas):
def __init__(self, boss=None, larg=1000, haut=200):
Canvas.__init__(self)
self.configure(width=larg, height=haut)
self.larg, self.haut = larg, haut
class Graphe(Canvas):
def __init__(self, boss=None,larg=1000, haut=1000):
Canvas.__init__(self)
self.configure(width=larg, height=haut)
self.larg, self.haut = larg, haut
class Combo(Canvas):
"Widget composite associant un champ d'entrée avec une boîte de liste"
def __init__(self, boss=None, larg=50, haut=50):
Canvas.__init__(self)
self.larg, self.haut = larg, haut
def attente():
fen = Tk()
text1 = Label(fen,text="Patientez quelques minutes", fg="red", font="Arial 30")
fen.geometry("%dx%d%+d%+d" % (600,100,300,300))
text1.grid(padx=40, pady=40,)
fen = mainloop()
main()
Dell XPS 13 Ubuntu 20.04 LTS. Lenovo Legion Ubuntu 20.04 LTS
Hors ligne
#2 Le 01/02/2017, à 15:25
- BadZoot
Re : Tkinter et threads parallèles
Aloha!
Est ce qu'une instance Toplevel() conviendrait?
Le pervers père de Pierre perd son imper' vert.
No, I am Zoot's identical twin-sister : Dingo.
Hors ligne
#3 Le 05/02/2017, à 20:12
- jmg17
Re : Tkinter et threads parallèles
Oui, après avoir un peu galéré, ça marche avec Topleve().
Merci pour ton aide.
Dell XPS 13 Ubuntu 20.04 LTS. Lenovo Legion Ubuntu 20.04 LTS
Hors ligne
Pages : 1