Search code examples
pythonpython-2.7termcolor

How to make a string blink for a specified time using the colored function in the termcolor module? [Python 2.7]


I am trying to figure out how I can use the blink attribute of the colored() function in the termcolor module to blink for a specific amount of time, and then stop.

I looked at the package index and the properties of this, but I haven't seen anywhere describing if this would be possible.

I currently have the following bit of code that makes the string blink indefinitely:

print(colored('Picking the first dealer by random...', 'cyan', attrs=['blink']))

I would like to be able to make this string blink for a couple of seconds, stop, and then continue running the program. Is there a possible way to do this?


Solution

  • This blink_once() function will show the word TEXT for .5 secs and will hide it for .5 secs afterwards.

    So the effect is just a one time blink. I do it writing spaces in he same place as the text after a delay. It's easy to modify the delay or the text, or adding them as parameters if you need it for your specific project.

    The blink(number) calls blink_once() a determined number of times. So blink(3) will show the word "TEXT" blinking 3 times.

     import sys
     import time
    
     def blink_once():
         sys.stdout.write('\rTEXT')
         time.sleep(0.5)
         b = ("Loading")
         sys.stdout.write('\r     ')
         time.sleep(0.5)
    
    def blink(number):
         for x in range(0,number):
             blink_once()
    
    blink(3)