PDA

View Full Version : [Curses][Python] Typed text in terminal is invisible _after running program_



fiddler616
March 6th, 2009, 02:42 AM
Hello,
Today I learned about curses--and found this tutorial.
To make sure I was doing things right, I made a simple Hello World! script:

import curses as c
stdscr = c.initscr()
c.noecho()
c.cbreak()
stdscr.keypad(1)
stdscr.addstr("Hello World!\n")
stdscr.refresh()

c.nocbreak(); stdscr.keypad(0); c.echo() #To return terminal back to normal
Which outputs:

Hello World!
timmy@Stanway01:~$

And then I can use my terminal like normal--except I can't see anything I type in. For example:

Hello World!
timmy@Stanway01:~$ Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> timmy@Stanway01:~$
I typed in python, and then exit(). And yes, it did that spacing.

How do I solve this? Thank you.

wmcbrine
March 6th, 2009, 05:08 AM
You need to call endwin() when you're done, before exiting the program. Your last line is nonsense; replace it with "c.endwin()".

Of course, this will clear the screen. To avoid that, insert a getch() before the endwin(). The screen will still be cleared, but at least you'll get a chance to read it. If you want to put up output that will remain when you exit the program, then curses isn't really what you want.

Meanwhile, if your program does exit without calling endwin(), you can run "reset" on the command line to restore the terminal state.

fiddler616
March 6th, 2009, 01:22 PM
Hmm...getch() and curses.getch() both raise error. curses.endwin() works though--thanks! (Although you're right--it does whizz by the screen)

wmcbrine
March 6th, 2009, 06:39 PM
In Python, getch() is a window method: stdscr.getch()

fiddler616
March 7th, 2009, 12:13 AM
In Python, getch() is a window method: stdscr.getch()
Ah, that'll do it.

Thanks a lot!

*Solved*