Nov-13-2018, 03:53 PM
Hello,
I work with python 3.7 and want to develop a litle animation allowing a dot to move thanks to recorded data.
i create 2 threads, one thread is used to "catch" new data (a simple incrementation but recorded from a tracking system later) and an other thread to run the dot move. I want to stop the 2 threads and the widget (fen) with an event (a button QUIT) but i don't succeed: any idea?
my code is below
Thanks!
I work with python 3.7 and want to develop a litle animation allowing a dot to move thanks to recorded data.
i create 2 threads, one thread is used to "catch" new data (a simple incrementation but recorded from a tracking system later) and an other thread to run the dot move. I want to stop the 2 threads and the widget (fen) with an event (a button QUIT) but i don't succeed: any idea?
my code is below
Thanks!
# moving point (see p 366 example in python book)
from tkinter import *
import time,threading
x,y= 10,10# global variable
class threadCollectData(threading.Thread):
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.00001
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):
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
fen=Tk()
fen.title('moving point')
can=Canvas(fen,width=400,height=250,bg="white")
can.pack(side=LEFT)
movingPoint=can.create_oval(10,10,20,20,fill='red')
t_C=threadCollectData()
t_M=threadMovingPoint(can,movingPoint)
Button(fen,text='Move',command=t_M.start).pack(side=BOTTOM)
#bou1=Button(fen,text='stop Thread 1', command=t_M.stop)
#bou1.pack(side=BOTTOM)
#bou2=Button(fen,text='stop Thread 2', command=t_C.stop)
#bou2.pack(side=BOTTOM)
t_C.start()
bou3=Button(fen,text='QUIT', command=???) # <-- how to close the threads t_M and t_C and fen?
bou3.pack(side=BOTTOM)
fen.mainloop() # Démarrage du gestionnaire d'évenements
fen.destroy()
