Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ncurses, python, and OSX Lion

I'm new to nurses, and trying it out on my OSX Lion with some python code. I've ran across a weird bug, and I don't know what I'm doing wrong. I've Googled extensively, and can't find a similar issue, even in linux. I've selectively removed lines to see if one of them is an issue, also. When I run the code below, I get nothing. No menu, and my terminal is messed up, if I hit enter, you see what I get in the picture below. I have to type a reset to make it work well again. Can anyone give me suggestions, or point me in the direction where to look? I would really appreciate it. Thanks.

Script:

import curses

screen = curses.initscr()   # Init curses
curses.noecho()             # Suppress key output to screen
curses.curs_set(0)          # remove cursor from screen
screen.keypad(1)            # set mode when capturing keypresses

top_pos = 12
left_pos = 12
screen.addstr(top_pos, left_pos, "This is a String")

Result:

screenshot

BTW, I'm using the default python and libs in Lion, no macports. I'd like to use the native libraries, if possible.

like image 901
John Avatar asked Jan 18 '12 05:01

John


1 Answers

You have 2 problems.

After adding the string to the screen with addstr you don't tell it to refresh the screen. Add this after the call to addstr:

screen.refresh()

You need to call endwin() at the end of you program to reset the terminal. Add this to the end of your program:

curses.endwin()

That said, after making those 2 changes when you run your program it will appear to do nothing because after displaying the string on the screen curses exits and returns the screen to the state before you ran the program.

Add this before the call to endwin():

screen.getch()

Then it will wait for you press a key before exiting.

like image 145
Craig Avatar answered Sep 24 '22 00:09

Craig