curses.nocbreak() stdscr.keypad( False ) curses.echo() from curses import wrapper def main(stdscr): # Clear screen stdscr.clear() # This raises ZeroDivisionError when i == 10. for i in range(0,11 ): v =i-10 stdscr.addstr(i,0,'10 divided by {} is {} '.format(v, 10 /v)) stdscr.refresh() stdscr.getkey() wrapper(main) begin_x=20 ; begin_y=7 height =5; width =40 win =curses.newwin(height, width, begin_y, begin_x) Note that the coordinate system used in curses is unusual. Coordinates are always passed in the order y,x , and the top-left corner of a window is coordinate (0,0). This breaks the normal convention for handling coordinates where the x coordinate comes first. This is an unfortunate difference from most other computer applications, but it’s been part of curses since it was first written, and it’s too late to change things now. Your application can determine the size of the screen by using the$_{curses.LINES}$and$_{curses.COLS}$variables to The$_{stdscr}$object returned by the$_{initscr()}$ function is a window object that covers the entire screen. Many programs may need only this single window, but you might wish to divide the screen into smaller windows, in order to redraw or clear them separately. The$_{newwin()}$ function creates a new window of a given size, returning the new window object. Windows are the basic abstraction in curses. A window object represents a rectangular area of the screen, and supports methods to display text, erase it, allow the user to input strings, and so forth. The$_{wrapper()}$ function takes a callable object and does the initializations described above, also initializing colors if color support is present. wrapper() then runs your provided callable. Once the callable returns,$_{wrapper()}$ will restore the original state of the terminal. The callable is called inside a$_{try}$…$_{except}$that catches exceptions, restores the state of the terminal, and then re-raises the exception. Therefore your terminal won’t be left in a funny state on exception and you’ll be able to read the exception’s message and traceback. In Python you can avoid these complications and make debugging much easier by importing the curses. wrapper() function and using it like this: A common problem when debugging a curses application is to get your terminal messed up when the application dies without restoring the terminal to its previous state. In Python this commonly happens when your code is buggy and raises an uncaught exception. Keys are no longer echoed to the screen when you type them, for example, which makes using the shell difficult. to reverse the curses-friendly terminal settings. Then call the endwin() function to restore the terminal to its original operating mode. curses.endwin() 3 Windows and Pads 3