0

In the program I've been working on in Python, I need to be able to print a list of elements one by one, going to a new line after n elements to form a grid. However, every time the program reprints the grid, you can see it progressing element by element, which looks rather ugly and distracting to the user. I was wondering if there was a way to "pause" the console output for a brief amount of time to allow the grid to be printed, then show the grid afterwards, erasing the previous printout, as to not show it printing element by element. The reason I need to do this is because the program uses Colorama for colored outputs, but different elements in the list will need to have different colors, meaning each element has to be printed one by one.

EDIT (Current code):

import time as t
from os import system as c
h = 50
w = 50
loop1 = 0
ostype = "Windows"
def cl():
  if(ostype == "Linux"):
    c('clear')
  if(ostype == "Windows"):
    c('cls')
def do():
    grid = []
    for x in range(0,h):
        temp = []
        for z in range(0,w):
            temp.append("#")
        grid.append(temp)
    for a in range(0,h):
        for b in range(0,w):
            print(grid[a][b], flush=False, end="")
        print()
while(loop1 == 0):
    do()
    t.sleep(1)
    cl()
Starspiker
  • 11
  • 2

1 Answers1

0

You can probably tell print to not to flush the standard out buffer, and have the last print to flush everything. Depends on what version of python you're using, for python 3 print function takes a flush argument, set that to true/false accordingly.

noobius
  • 1,435
  • 5
  • 13
  • I've edited above with a test program that isn't working as I need it to. I've never used the flush option before, so I'm not quite sure how to use it. – Starspiker Dec 03 '19 at 16:41
  • You're probably in the rendering synchronization, refresh rate territory that the OS is doing. Which you probably don't have much control with regular python. You may want to look into more of a graphical/UI rendering. Python may have some modules to handle this, pygame, pyopengl etc... those I'm pretty sure will let you have more control regarding rendering capabilities. – noobius Dec 03 '19 at 18:07