4

Who can help me on this?

Pause Countdown when press "P" Key and continue the Countdown when press "S" key. Until now i have this code, but i cant find the way to solve this.

Thanks

from multiprocessing import Process
import keyboard
import time


def countdown_1():
    i=6
    j=40
    k=0
    while True:
        a = keyboard.read_key()

        if(str(a) != "p"):    
            if(j==-1):
                j=59
                i -=1
            if(j > 9):  

                print(str(k)+str(i)+":"+str(j))
            else:

                print(str(k)+str(i)+":"+str(k)+str(j))

            time.sleep(1)
            j -= 1
            if(i==0 and j==-1):
                break
    if(i==0 and j==-1):
        print("END")
        time.sleep(1)


countdown_1()
1
  • There is a similar solved issue here. Hope it helps Commented Feb 18, 2020 at 13:14

1 Answer 1

2

I get a solution to your problem, that is because when you use keyboard.readkey() python wait for a key to be pressed. instead, you should use keyboard.is_pressed('X')

I have modified your code to make a working version, I slightly change it to match my taste.

from multiprocessing import Process
import keyboard
import time


def countdown_1():
    pause_keyboard = False  # I use a bolean as a state is clearer for me
    i = 6  # minutes
    j = 40
    k = 0  # represent 0 we will instead use code format

    while True:

        starting_time = time.time()

        while True:  # this loop wait one second or slightly more

            if time.time() - starting_time >= 1: #  a second or more
                break

            if keyboard.is_pressed('p'):
                pause_keyboard = True

            elif keyboard.is_pressed('s'):
                pause_keyboard = False

        if pause_keyboard:
            continue


        if (j == -1): ## here we adjust the count when we changes minutes

            j = 59  # 59 secondes
            i -= 1  # one minutes less

        if(j > 9):  ## in order to pretty print

            print("{}{}:{}".format(0, i, j))  # you can direclty use 0 instead of k.
        else:
            print("{}{}:{}{}".format(0, i, 0, j))

        j -= 1

        if(i==0 and j==-1):  # we finish the counter
            break

    if(i==0 and j==-1):
        print("END")
        time.sleep(1) # wait a last second


countdown_1()

EDIT: Use time.time() instead of sleep to be able to catch signals.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks... im running This on windows, but not stop when i press pause.. it works for you?
I tested it on ubuntu and it worked. sometimes the input is not recognized but I believe (without certainty) that is caused by sleep()...
Any other way? sorry :(
I am editing the code to use time instead of sleep.
dont run... Traceback (most recent call last): File "C:\Users\innov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec(codeObject, main.__dict__) File "C:\Users\innov\Desktop\DISE-API\Weather\KEYBOARD.py", line 52, in <module> countdown_1() File "C:\Users\innov\Desktop\DISE-API\Weather\KEYBOARD.py", line 18, in countdown_1 if time.time() - starting_time() >= 1: # a second or more TypeError: 'float' object is not callable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.