I'm fairly new to python and am trying to make a basic stopwatch script. I want to be able to pause the stopwatch, but the time.sleep(1)
code which waits for the next number to be shown interferes with the keyboard event. BTW I'm using the python package 'keyboard' to get the event. I have tried threading but I can't pause the other thread as far as I know.
Here's my code:
import time, keyboard, sys
print('Stopwatch \n')
input('Press enter to start:')
def counter():
stopwatch = 0
while True:
time.sleep(1)
stopwatch += 1
print(stopwatch)
question()
def question():
while True:
if keyboard.is_pressed('p'):
print('\nPaused')
input('Press enter to resume:')
if keyboard.is_pressed('s'):
print('\nStopped')
sys.exit()
counter()
I don't necessarily need the question
function, but was experimenting to see if I could get it to work. I could combine the two functions.