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,

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? :)

Jeff

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.

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.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

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.

Jeff

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()

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

Jeff

using sys.stdout



#!/usr/bin/env python

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


sys.stdout.write('\b\bbb')
sys.stdout.flush()
time.sleep(2)

sys.stdout.write('\b\bcc')
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.