0

I'm doing a Ascii module. So I want to make a load bar that update every seconds. But with "os.system('cls')", it's blinking the screen. So what can I do ?

os.system('cls')
sys.stdout.write(string)
Gkcrafting
  • 26
  • 1
  • 2

1 Answers1

-1

I figured it out... You can use ANSI codes to move the cursor then clear the lines without any BLINK!

print('\033[4A\033[2K', end='')

\033[4A Moves the cursor 4 lines up (\033[{lines}A you can replace lines with however many you need) \033[2K Clears all those lines without the screen blinking. You can use it in a simple typewrite function that needs a constant message or a box around it like this:

from time import sleep   

def typewrite(text: str):
    lines = text.split('\n')
    for line in lines:
        display = ''
        for char in line:
            display += char
            print(f'╭─ SOME MESSAGE OR SOMEONES NAME ────────────────────────────────────────────╮')
            print(f'│ {display:74} │') # :74 is the same as ' ' * 74
            print(f'╰────────────────────────────────────────────────────────────────────────────╯')
            
            sleep(0.05)
            
            print('\033[3A\033[2K', end='')

The only problem with this is that the top line is blinking. To fix this all we need to do is to add a empty line that is blinking so the user cant see it. We also move the cursor up from 3 to 4 lines.

def typewrite(text: str):
    lines = text.split('\n')
    for line in lines:
        display = ''
        for char in line:
            display += char
            print('')
            print(f'╭─ SOME MESSAGE OR SOMEONES NAME ────────────────────────────────────────────╮')
            print(f'│ {display:74} │') # :74 is the same as ' ' * 74
            print(f'╰────────────────────────────────────────────────────────────────────────────╯')
            
            sleep(0.05)
            
            print('\033[4A\033[2K', end='')

To make this into your code just print your text and add a print('') at the start. Then use this print('\033[4A\033[2K', end='') but change the 4 to however many lines that you printed including the print(''). Then it should work without blinking.

If you want to hide the cursor you can use this gibberish or make the cursor the same color as the background:

import ctypes

if os.name == 'nt':
    class _CursorInfo(ctypes.Structure):
        _fields_ = [("size", ctypes.c_int),
                    ("visible", ctypes.c_byte)]
def hide_cursor() -> None:
    if os.name == 'nt':
        ci = _CursorInfo()
        handle = ctypes.windll.kernel32.GetStdHandle(-11)
        ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
        ci.visible = False
        ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
def show_cursor() -> None:
    if os.name == 'nt':
        ci = _CursorInfo()
        handle = ctypes.windll.kernel32.GetStdHandle(-11)
        ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
        ci.visible = True
        ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))

Note: All of this is still new to me so I am still testing this out to fully understand it.

  • If you want I can create something for you – DarkDominator Aug 18 '22 at 00:27
  • Welcome to Stack Overflow. [Please don't post](https://meta.stackexchange.com/q/104227/248627) the [same](https://stackoverflow.com/a/73396379/354577) [answer](https://stackoverflow.com/a/73396206/354577) [multiple](https://stackoverflow.com/a/73396032/354577) times. – ChrisGPT was on strike Aug 21 '22 at 18:51