dassouki
dassouki

Reputation: 6366

Python: simple CLI GUI

A simple question on a python module. Let's say I have the following code:

for i in range(1000):
  print i

It'll output something along the lines of:

1

2

'Snip'

999

Is it possible to have the program output all the numbers on the same line? I'm not talking about "1, 2, 3 .." rather I want the line value to change to the current i

Upvotes: 0

Views: 4005

Answers (5)

VMDX
VMDX

Reputation: 685

Text printed to stdout can't be dynamically changed. You need to use a panel.

http://docs.python.org/library/curses.panel.html

Initialize a new panel, and then use Panel.set_userptr(obj) in your loop.

Upvotes: 1

Mike Sherov
Mike Sherov

Reputation: 13427

import os

for i in range(1000):
  print i
  os.system("clear")

edit: as mentioned in the comment below, change "clear" to "cls" if using windows.

Upvotes: 2

SilentGhost
SilentGhost

Reputation: 320009

For a simple case the following code works just fine:

sys.stdout.write(str(i)+'\r')
sys.stdout.flush()

Upvotes: 2

ewall
ewall

Reputation: 28128

If you want the character to be overwritten/replaced each time, you may need to use a terminal control library like 'curses'. Here's a Python how-to article to get you started.

Upvotes: 3

Max Shawabkeh
Max Shawabkeh

Reputation: 38683

If you want to draw a GUI inside a terminal, you'll have to use the curses module.

Upvotes: 3

Related Questions