0

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,

8
Contributors
20
Replies
22
Views
10 Years
Discussion Span
Last Post by mchen10
0

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.

0

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?

0

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.

0

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.

0

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.

0

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.

0

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

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

Jeff

0

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.

0

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.

0

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

0

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.

0

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.

0

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

0

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()
0
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')
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.