Implementing a backspace in Python 3.3.2 Shell using Idle [duplicate]
Asked Answered
I

2

1

There are several folks on here looking for backspace answers in Python. None of the questions I have searched have answered this for me, so here goes:

The Simple Goal: be able to print out a status string on one line, where the next status overwrites the first. Similar to a % complete status, where instead of scrolling a long line of 1%\n, 2%, ... etc. we just overwrite the first line with the newest value.

Now the question. When I type this in idle: print("a\bc") I get this as output: ac with what looks like an odd box with a circle between the 'a' and 'c'. The same thing happens when using sys.stdout.write().

Is this an Idle editor setting/issue? Does anyone even know if what I am trying is possible in the Idle Shell?

Thanks for any insight.

PS: Running Python 3.3.2 Idle on Windows 7, 64-bit system.

EDIT: Copying the output in Notepad++ is revealing that Python is printing out a 'backspace' character, and not actually going back a space. Perhaps what I am trying to accomplish is not possible?

Interracial answered 4/10, 2013 at 17:57 Comment(1)
You cannot currently get what you want in the IDLE shell. Because of SO questions like this one, I am considering changing that.Spiegel
S
3

Edit:

Apparently the carriage return \r and the backspace \b won't actually work within Idle because it uses a text control that doesn't render return/backspace properly.

You might be able to write some sort of patch for Idle, but it might be more trouble than it's worth (unless you really like Idle)

Salk answered 4/10, 2013 at 19:4 Comment(6)
When I try this, I am not seeing the previous text becoming overridden. My output is: 0 wonderful chickens! 1 wonderful chickens! 2 wonderful chickens! 3 wonderful chickens! 4 wonderful chickens! 5 wonderful chickens! 6 wonderful chickens! 7 wonderful chickens! 8 wonderful chickens! 9 wonderful chickens!Interracial
This answer strikes me as somewhat sarcastic, and isn't even helpful despite that.Arvid
Not sarcastic at all - it might just be that Idle doesn't render carriage returns properly because this works perfectly in the cmd window in Windows. Also in the interactive interpreter. Based on @Nanomurf's comment it would appear that Idle's shell won't actually do this.Salk
@Arvid - misguided, apparently. As I didn't actually bother to open Idle (because I find it way to quirky) and try out my code. I expect it has to do with the nature of the Tk text control that the code lives within.Salk
I appreciate the feedback. I shouldn't be lazy in my Python and stick with the simple, comfortable confines of Idle... Thank you for your 2 cents!Interracial
Terminals do not agree what rendering \b 'properly' means. IDLE currently outputs it as is and you see whatever tcl/tk Text does on your system. I am considering replacing that with something more consistent and informative and adding a terminal mode that interprets \b in one of the two possible ways (move cursor left, and also delete char left).Spiegel
A
0

This doesn't answer your question in a literal fashion, but I think it might be useful to point out that generally interfaces like the one where you are describing (e.g., where one part of the screen is continuously updated, without newlines), it just generally implemented using a library like ncurses.

Python has a curses library built-in (http://docs.python.org/3.3/library/curses.html), which can more or less achieve your end goal.

Arvid answered 4/10, 2013 at 19:27 Comment(3)
Are you sure that the curses library will work in IDLE? And, does curses work in MS Windows?Requite
The Python docs say "The Windows version of Python doesn’t include the curses module. A ported version called UniCurses is available.", so it should be possible to get it working. IDLE is just an IDE, so I don't really see what it has to do with the interpreter's functionality.Arvid
Curses expects to its output to go to a real 'terminal', or terminal emulator program, most likely one that responds to VT100/ANSI. IDLE's shell does not (currently) add such an interface to a tk/tkinter Text widget.Spiegel

© 2022 - 2024 — McMap. All rights reserved.