0

i am trying to make a simple RGB animation with python, and I am having some difficulties.

The problem really is the output, that is completely wrong as what I wanted.

Code:

def animation(message):
    def yuh():
        while True:
            colors = dict(Fore.__dict__.items())
            for color, i in zip(colors.keys(), range(20)):
                sys.stdout.write(colors[color] + message + "\r")
                sys.stdout.flush()
                sys.stdout.write('\b')
                time.sleep(0.5)
    threading.Thread(target=yuh).start()


def menu():
    animation("Hello Please select a option !")
    print("1 -- Test")
    qa = input("Answer?: ")

    if qa == 1:
        print("You did it !")
        sys.exit()

menu()

Output:

1 -- Test
Hello Please select a option !a option !

My initial thoughts werw that the output looked like this:

Hello Please select a option !
1 -- Test
Answer?: 

How am I able to do this?

Maritn Ge
  • 997
  • 8
  • 35
AzgarD
  • 27
  • 1
  • 6

1 Answers1

0

This is because the cursor stays where the last print/input function ended. So after line 3 of menu(), the cursor is at the end of "Answer?:", where the message is printed first, the after the "\r" carriage return pulls the cursor to the start of the line. There is a solution though:

def animation(message):
        def yuh():
                while True:
                        colors = dict(Fore.__dict__.items())
                        for color, i in zip(colors.keys(), range(20)):
                                sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (0, 0, colors[color] + message + "\r"))
                                sys.stdout.flush()
                                sys.stdout.write('\b')
                                time.sleep(0.5)
        threading.Thread(target=yuh).start()


def menu():
        animation("Hello Please select a option !")
        print("1 -- Test")
        qa = input("Answer?: ")

        if qa == 1:
                print("You did it !")
                sys.exit()

menu()

You might need to edit the coordinates but other than that it should work!

Help from: Is it possible to print a string at a certain screen position inside IDLE?

Vedant36
  • 318
  • 1
  • 6
  • The output is a little bit better, but still not great... Output (I changed the coordinates to 1,1) ```8llo Please select a option ! 1 -- Test Answer?: 7``` – AzgarD Dec 26 '20 at 18:46
  • EDIT: I still didn't understanded very well the coordinates, so i maybe wrong. – AzgarD Dec 26 '20 at 18:53
  • After some time, a found a ANSII code that worked, here is it: \x1b7\x1b\x1b[F\x1b[F[%d;%df%s\x1b8 – AzgarD Dec 28 '20 at 22:39