'Python - Clearing Command Prompt From Certain Line?
I have a script in python which prints a list of lists like so:
list_of_lists = [["a", "b", "c", "d"],
["a", "b", "c", "d"],
["a", "b", "c", "d"]]
def print_lists_of_lists(list_of_lists):
for list in list_of_lists:
print()
for char in list:
print(char, end = "")
Using this function I get an output that looks like this:
abcd
abcd
abcd
Inputs change the contents of these lists. Each time the user makes an input, I clear the command prompt and then reprint the list. This works fine, but if the user spams the inputs the screen "flickers" due to the print process being unable to keep up with inputs.
My idea for a solution is to go to the specific line in the command prompt where the contents of the list changed...
abcd
abcc #So from here down is cleared and reprinted
abcd #because the d at the end of the line changed to a c
...clear the screen from that line and then reprint the list from the same line. This way, only part of the screen changes, leading to less flickering.
But I have no idea how to do that. I've tried ansi codes to move the cursor, looked into curses implementations for windows, but I can't seem to find any way to do this. Any thoughts?
Edit: In pseudo code this is what I have:
while True:
clear_screen
print_screen
wait_for_keypress
So if the user holds down a key, this loop goes incredibly fast, making the screen "flicker".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
