Python Forum
How to immediately kill and restart a thread while using a time.sleep() inside it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to immediately kill and restart a thread while using a time.sleep() inside it?
#1
I made this little example where a cursor is blinking on the canvas:
from tkinter import *
from threading import Thread
from time import sleep
 
root = Tk()
 
canvas = Canvas(root)
canvas.pack()

cursor_blink = True

def cursor():
    while cursor_blink:
        canvas.create_line(100,100,100,200,tag='cursor')
        sleep(0.5)
        canvas.delete('cursor')
        sleep(0.5)

thread = Thread(target=cursor)
thread.start()

def reset_cursor(evt):
    # kill cursor thread? / this function is supposed to restart the thread; how?
    thread.start()

root.bind('<ButtonRelease-1>', reset_cursor)
 
root.mainloop()
The goal is to reset the cursor immediately when clicking on the canvas. Currently it waits for the loop to complete before checking if the curs variable is still True. Is there maybe a different way without using sleep() to make the cursor blink?
Reply
#2
Instead of blocking the thread with sleep use time as a condition
import time

start=time.time()
end=0
elapsed=0
x=0

while True:

	if 0.0 <=elapsed <=0.5:
		print("On")
	elif 0.5 <=elapsed <=1:
		print("Off")
	elif elapsed >1:
		start=time.time()
		x+=1

	end=time.time()
	elapsed=end-start

	if x>=10:
		break
Reply
#3
I wrote a blinker using the tkinter after() method, without threads. Normally, threads other than the main thread should not update the state of the GUI
from tkinter import *
  
root = Tk()
  
canvas = Canvas(root)
canvas.pack()

class Blinker:
    def __init__(self, canvas, tag):
        self.canvas = canvas
        self.tag = tag
        self.delay = 500
        self.job = None
        
    def _negate(self, state):
        return 'hidden' if state == 'normal' else 'normal'
        
    def start(self, state='normal'):
        if self.job is not None:
            self.canvas.after_cancel(self.job)
            self.job = None
        self.canvas.itemconfigure(self.tag, state=state)
        self.job = self.canvas.after(self.delay, self.toggle)
        
    def toggle(self):
        state = self.canvas.itemcget(self.tag, 'state')
        self.canvas.itemconfigure(self.tag, state=self._negate(state))
        self.job = self.canvas.after(self.delay, self.toggle)
        
    def stop(self, state=None):
        if self.job is not None:
            self.canvas.after_cancel(self.job)
            self.job = None
        if state is not None:
            self.canvas.itemconfigure(self.tag, state=state)

    def reset(self, event):
        self.start()

canvas.create_line(100,100,100,200,tag='cursor')
blinker = Blinker(canvas, 'cursor')
blinker.start()
 
root.bind('<ButtonRelease-1>', blinker.reset)
  
root.mainloop()
Reply
#4
(Feb-03-2022, 08:32 PM)Gribouillis Wrote: Normally, threads other than the main thread should not update the state of the GUI
Why is that? why should I not do that?
Reply
#5
Actually, I'm probably wrong in this belief. However, read the paragraph about the threading model in the Tkinter documentation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 225 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  Use subprocess.Popen and time.sleep chucky831 2 1,931 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Time.sleep: stop appending item to the list if time is early quest 0 1,867 Apr-13-2021, 11:44 AM
Last Post: quest
  Determine number of all the immediately adjacent points in python zackk 1 1,854 Feb-06-2021, 09:23 AM
Last Post: zackk
  Can you end the Time.sleep function boier96 9 9,395 Jan-16-2021, 10:09 PM
Last Post: Serafim
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,764 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  Using a button to kill and restart a script duckredbeard 3 3,300 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  time.sleep mtnwinds 4 2,845 May-21-2020, 10:12 AM
Last Post: Larz60+
  Hotkey to restart code? DannyB 1 2,739 May-20-2020, 02:52 AM
Last Post: michael1789
  Updating a matrix in a time interval inside a for loop vp1989 4 2,883 May-17-2020, 07:15 PM
Last Post: vp1989

Forum Jump:

User Panel Messages

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