Print slow in python but in a box

Question:

I need to type some text inside a box but the text needs to be like typing but the box to just be thare. I also need it to be yellow.

My code:


from termcolor import colored
import sys, time, random
import os
os.system('clear')

def print_slow(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(0.1)




print(colored('----------------', 'yellow'))
print_slow('   |  something.  |')
print(colored('|              |', 'yellow'))
print(colored('----------------', 'yellow'))

I tried adding the two together but did not work.

*I can get the color part to work just I need to print the text slowly and the box not.

Asked By: thegamer1234 f

||

Answers:

Here’s one way to do it:

from termcolor import colored
import sys, time, random
import os

os.system("cls")


def getText(i, str):
    totalLength = 12
    subString = str[:i]
    spaces = " " * (totalLength - i)
    return subString + spaces


def print_slow(string):
    for i in range(len(string)):
        time.sleep(0.1)
        os.system("cls")
        print(colored("----------------", "yellow"))
        print(colored(f"| {getText(i,string)} |", "yellow"))
        print(colored("----------------", "yellow"))


print_slow("Hello World!")
Answered By: Barnaby

Since your code runs(and print the text) line by line, it would not be drawn as you want.

You may want to check out curse-like libraries such as https://docs.python.org/3/library/curses.html
https://docs.python.org/3/howto/curses.html

Answered By: Younggun Kim
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.