3
votes

I wrote a small program in python and outputted some screen display using the curses library. For my simple output this seems to work. I run my python program from the command line.

My problem is that if I kill the python program the terminal doesn't properly display. For example: 'ls -al' displays properly before I run my python curses program 'ls -al' does not display properly after I kill the python curses program.

What can I do to make my terminal display output properly after I kill my python curses program?

5

5 Answers

7
votes

Usually the reset command will reset your terminal settings to default values.

6
votes

If you use curses.wrapper, it will handle all the cleanup (and set up) for you. http://docs.python.org/library/curses.html#curses.wrapper

4
votes

Initialize the curses the following way, it will handle a cleanup.

class curses_screen:
    def __enter__(self):
        self.stdscr = curses.initscr()
        curses.cbreak()
        curses.noecho()
        self.stdscr.keypad(1)
        SCREEN_HEIGHT, SCREEN_WIDTH = self.stdscr.getmaxyx()
        return self.stdscr
    def __exit__(self,a,b,c):
        curses.nocbreak()
        self.stdscr.keypad(0)
        curses.echo()
        curses.endwin()

with curses_screen() as stdscr:
"""
Execution code plush getch code here
"""
1
votes

Register a signal handler that will uninitialize curses.

1
votes

I think you should use curses.endwin(). It restores the terminal window...
In fact if you don't call it after program is closed terminal will show everything like it is in the curses window...