Bonjour,
J'écrit un code en python, l'objectif final étant de créer 2 threads: un qui gère la collecte des données d'un matériel et l'autre qui anime l'object via les données recueillies. Pour l'instant je simule la collecte des données avec une simple translation de mon point selon l'axe horizontal.
Je lance mon code, j'appuie sur l'onglet 'move' et tout se passe bien, le problème survient lorsque je ferme la fenêtre (widget frame), ce qui est sensé "tuer" les 2 threads, mais cela ne fonctionne pas et mon code semble continuer à tourner.
Mon code:
et mon message d'erreur:
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
44
45
46
47
48
49
50
51
52 from tkinter import * import time,threading x,y= 10,10# global variable class threadCollectData(threading.Thread):# threadCollectData is inherited from threading.Thread class def __init__(self): threading.Thread.__init__(self) self.collect=1 def run(self): global x,y # to modify x, y in a function while self.collect==1: x+=0.000001 y=y def stop(self): # method to stop the thread self.collect=0 class threadMovingPoint(threading.Thread): def __init__(self,canevas,Mvt_pt): threading.Thread.__init__(self) self.can=canevas self.Mvt_pt=Mvt_pt # self.anim=1 #flag to launch animation def run(self): global x,y while self.anim==1: self.can.coords(self.Mvt_pt,x-5,y-5,x+5,y+5) time.sleep(1) def stop(self): self.anim=0 class App(Frame):#App class is based on the class Frame from tkinter: App INHERIT class Frame def __init__(self): Frame.__init__(self)# class App have to be initialized explicitly as a frame self.pack() # so App explicitly calls its parent class's __init__() method can=Canvas(self,width=400,height=250,bg="white") can.pack() movingPoint=can.create_oval(10,10,20,20,fill='red') t_C=threadCollectData() t_M=threadMovingPoint(can,movingPoint) t_C.start() Button(self,text='Move',command=t_M.start).pack(side=LEFT) self.bind('<Destroy>',t_M.stop) # Event 'Destroy' (Widget destroy) causes thread to stop self.bind('<Destroy>',t_C.stop) App().mainloop() #Launch event receptionist associated with App class
Une idée du problème dans mon code?Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\jjeuvre\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: stop() takes 1 positional argument but 2 were given
>>> Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\jjeuvre\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "P:\Mes_Doc_reseau\recherche\Programmes\python\dpl_point.py", line 32, in run
self.can.coords(self.Mvt_pt,x-5,y-5,x+5,y+5)
File "C:\Users\jjeuvre\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2469, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!app.!canvas"
merci d'avance.
Partager