2

i like to output each letter of a string after waiting some time, to get a typewriter effect.

for char in string:
     libtcod.console_print(0,3,3,char)
     time.sleep(50)

But this blocks the main thread, and the program turns inactive.
You cant access it anymore until it finishes
Note: libtcod is used

1

1 Answer 1

7

Unless there is something preventing you from doing so, just put it into a thread.

import threading
import time

class Typewriter(threading.Thread):
    def __init__(self, your_string):
        threading.Thread.__init__(self)
        self.my_string = your_string

    def run(self):
        for char in self.my_string:
            libtcod.console_print(0,3,3,char)
            time.sleep(50)

# make it type!
typer = Typewriter(your_string)
typer.start()
# wait for it to finish
typer.join()

This will prevent the sleep blocking your main function.

The documentation for threading can be found here
A decent example can be found here

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.