Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stop multiple threads
#1
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!
# 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()
	
		
Reply


Messages In This Thread
stop multiple threads - by jeuvrey - Nov-13-2018, 03:53 PM
RE: stop multiple threads - by Gribouillis - Nov-13-2018, 04:23 PM
RE: stop multiple threads - by jeuvrey - Nov-14-2018, 09:29 AM
RE: stop multiple threads - by Gribouillis - Nov-14-2018, 11:41 AM
RE: stop multiple threads - by woooee - Nov-14-2018, 05:41 PM
RE: stop multiple threads - by jeuvrey - Nov-15-2018, 01:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What to do to stop all these threads? Valjean 3 2,006 Oct-02-2023, 01:50 PM
Last Post: deanhystad
  Trying to separate a loop across multiple threads stylingpat 0 2,206 May-05-2021, 05:21 PM
Last Post: stylingpat
  Get average of multiple threads Contra_Boy 1 23,135 May-05-2020, 04:51 PM
Last Post: deanhystad
  Quitting multiple threads MuntyScruntfundle 3 3,522 Oct-17-2018, 05:14 AM
Last Post: volcano63
  How to run multiple threads properly cyberion1985 6 8,357 Dec-29-2017, 09:45 AM
Last Post: cyberion1985

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020