Bonjour,
Je coince actuellement sur un problème avec l'utilisation de Tkinter et des threads.
Le code ci-après résume mon problème.
Je créé une Gui qui lance une application qui elle-même lance plusieurs threads (ici deux) qui se partagent le même widget tkinter (ici un label) à mettre à jour. Ce code semble fonctionner mais plante au bout d'un certain nombre d'itérations (entre 0 et 10000+).
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 import threading from Tkinter import * import time class Th(threading.Thread): def __init__(self, label): threading.Thread.__init__(self) self.label = label self.index = 0 self.lock = threading.Lock() def run(self): while True: self.lock.acquire() time.sleep(0.01) self.label.config(text=str(self.index)) self.index += 1 self.lock.release() class App(): def __init__(self, label): self.label = label def run(self): Th(self.label).start() Th(self.label).start() class Gui(): def __init__(self): gui = Tk() label = Label(gui) label.pack() app = App(label) run = Button(gui, text="Run", command=app.run) run.pack() gui.mainloop() Gui()
Les messages d'erreurs sont:
ou
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner self.run() File "C:\Users\Pierre\workspace\Driver-Events\tests\threadTkinter.py", line 22, in run self.label.config(text=str(self.index)) File "C:\Python27\lib\lib-tk\Tkinter.py", line 1202, in configure return self._configure('configure', cnf, kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 1193, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) TclError: invalid command name "-text"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner self.run() File "C:\Users\Pierre\workspace\Driver-Events\tests\threadTkinter.py", line 22, in run self.label.config(text=str(self.index)) File "C:\Python27\lib\lib-tk\Tkinter.py", line 1202, in configure return self._configure('configure', cnf, kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 1193, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) TclError: bad option "590": must be cget or configure
Je souhaiterais votre avis sur ce qui ne vas pas.
Partager