If I print something to the screen, is it at all possible to erase some of that? Some sort of backspace? That way it would be possible to clear the screen, make a text progress bar, etc.

If not, what's the easiest way to do something similar?


Yes, there is a backspace character '\b', but not all IDEs will have the right output window for it:

# backspace twice and overwrite with space
print '1234567\b\b  '

raw_input('press enter')  # wait

The standard DOS command window will work.

Is there any way to erase things that have already been printed to the screen? Instead of re-printing the updated value, to actually change the one that's already there?

With a console program that is tough, now you have to invent a gotoxy(x,y) equivalent to put the cursor on the spot and overwrite from there. Maybe module curses has something like that?

The old Turbo C used to have a gotoxy(x,y) function.

Wouldn't it be easier to use the Tkinter Text widget for output?

It seems it would be easier to use tkinter text widget, but I was just wondering if there was a simple way to do that. Like an erase or something.

There is a WinAPI function to put the cursor into a specified location on the console. I have used it in a C++ program a long time ago. I am getting too old to remember the details.

BOOL SetCursorPos(
    int X, // horizontal position  
    int Y  // vertical position

One ought to be able to use it in Python too, check the Win32 extensions.

If you know in which windows dll the function SetCursorPos(x, y) is situated, then you can use module ctypes to get access to it.

Surely this is too hard? What are you trying to do, Matt?

Also, Vega ... how old is 'too old', exactly? :)


Surely this is too hard? What are you trying to do, Matt?

Also, Vega ... how old is 'too old', exactly? :)


It definetly seems to be too hard, doesn't it? For the reward anyway. I wanted to do things like a text progress bar in the console, I thought maybe there would be a way to erase already written text.

Too old to remember!

The old Turbo C used to have all that stuff in it, but then the console was king!
You may want to post that problem in the C/C++ forum. I think Narue used to have a reluctant solution.

The people in c/c++ will know how to do it python?

Maybe I'm misunderstanding, but if I wanted a text progress bar, I would use Tkinter and something like this:

from Tkinter import *

class MyFrame(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.master = master
        self.string = "The Quick Brown Fox jumped over the Lazy Dog."
        self.label = Label(self,
                           text="Type the following sentence:\n%s\nand hit Enter." % self.string)
        self.text = Text(self, height=4,width=40)
        self.progress_label = Label(self, text="Your progress: ")
        self.progress_bar = Label(self)
        self.progress_bar.var = StringVar()
        self.progress_bar['textvariable'] = self.progress_bar.var

        self.text.bind('<Any-KeyPress>', self.check_text)

        self.progress_label.grid(row = 2, column=0,sticky=W)

    def check_text(self,event=None):
        progress = ""
        string = self.text.get('0.0',END).strip('\n')  # strip needed b/c
                                                       # Text adds an extra \n.

        if event.keysym == 'Return':
            if string == self.string:
                self.progress_bar.var.set("YOU WIN!!!")
                self.progress_bar.var.set("YOU'RE NOT A WINNER THIS TIME.")
        elif string == "":
        for char in self.string:
            if char == string[0]:
                progress += "*"
                progress += "X"
            string = string[1:]
            if not string:

mainw = Tk()
mainw.f = MyFrame(mainw)

The user types the required string and hits enter, and check_text() does the updating. Hope this is useful.


I was just wondering if it could be done in the console. I know when I used a linux machine and downloaded some stuff through the terminal, it was similar to the console and had progress bars made out of text.

The people in c/c++ will know how to do it python?

Once you have the C code, you have something to work with. You can always embed C code into Python.

I'm disappointed in the article. I was looking forward to running Python in 'hell-mode'. :)

Seriously, if you have control over all of the text currently on screen, you could keep track of the lines, clear the screen, and re-print the ones you want. Bleah.


Finally found my old Python notes from school and followed it up. The module WConio pretty nuch gives you all the console thingies that Turbo C had. Take a look at:

Here is a taste ...

import WConio
import time
WConio.gotoxy(0, 0)
for c in 'Hallo there!':
WConio.gotoxy(6, 0)
for c in 'spamspamspam!':
WConio.gotoxy(10, 0)
for c in ' and eggs!   ':
WConio.gotoxy(20, 0)

Nice! Apparently, even antique batteries are included...


using sys.stdout

#!/usr/bin/env python

import sys, time
sys.stdout.write('aaa'); sys.stdout.flush(); time.sleep(2)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.