954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Possible to erase in the console?

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?

Thanks,

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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?

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

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

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

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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

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

Jeff

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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 
The people in c/c++ will know how to do it python?

Doubtful, probably because the same problem persists. Different operating systems = different problems.

There is no standard way for it to work. Try here if you ain't worried about portability. http://www.velocityreviews.com/forums/t349178-how-to-clear-screen-in-python-interactive-shell-mode.html

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.label.grid(columnspan=2)
        self.text.grid(columnspan=2)
        self.progress_label.grid(row = 2, column=0,sticky=W)
        self.progress_bar.grid(row=2,column=1,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!!!")
                return
            else:
                self.progress_bar.var.set("YOU'RE NOT A WINNER THIS TIME.")
                return
        elif string == "":
            return
        for char in self.string:
            if char == string[0]:
                progress += "*"
            else:
                progress += "X"
            string = string[1:]
            if not string:
                break
        self.progress_bar.var.set(progress.ljust(len(self.string)))

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


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

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

Doubtful, probably because the same problem persists. Different operating systems = different problems.

There is no standard way for it to work. Try here if you ain't worried about portability.

http://www.velocityreviews.com/forums/t349178-how-to-clear-screen-in-python-interactive-shell-mode.html

That does work, but is there a way to clear only a portion of the screen, one line?

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 
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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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:
http://newcenturycomputers.net/projects/wconio.html

Here is a taste ...

import WConio
import time
 
WConio.gotoxy(0, 0)
for c in 'Hallo there!':
    WConio.cputs(c)
    time.sleep(0.1)
 
WConio.gotoxy(6, 0)
for c in 'spamspamspam!':
    WConio.cputs(c)
    time.sleep(0.2)
 
WConio.gotoxy(10, 0)
for c in ' and eggs!   ':
    WConio.cputs(c)
    time.sleep(0.2)
    
WConio.gotoxy(20, 0)
 
raw_input()
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You