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
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
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
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
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
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
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
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