0

I am new to python coding (using pyscripter with the latest version of python (3.5)). I am wondering how to run 2 loops or scripts at the same time. IM using the graphics.py module and the time module. I need to open a window, create a countdown, and do a bunch of other stuff at the same time, but the problem is when i try t create a countdown using "time.sleep", it pauses the rest of the program. How would i go about creating a timer/countdown without pausing the rest of my script? Also it needs to be able to draw on the same window. Thank you very much.

2
  • 1
    You might want to look into the pygame module: pygame.org/wiki/GettingStarted . It's not just for games - it's a generic event loop. Commented Sep 25, 2017 at 0:59
  • Welcome on stackoverflow.com. Please always try to provide a Minimal, Complete, and Verifiable example that shows where you got stuck. Commented Sep 25, 2017 at 1:08

1 Answer 1

1

You can create a (worker) thread to do some extra work without blocking the rest of your code.

Here is some basic code (below) to get you started and a how-to.

from threading import Thread
from time import sleep

def threaded_function(arg):
    for i in range(arg):
        print("running")
        sleep(1)   

if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()
    print("thread finished...exiting")
Sign up to request clarification or add additional context in comments.

Comments

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.