Skip to content

Instantly share code, notes, and snippets.

@myano myano/cs.py
Created Jun 30, 2011

Embed
What would you like to do?
Quick example of ncurses in Python!
#!/usr/bin/env python
import curses
import curses.textpad
import time
stdscr = curses.initscr()
#curses.noecho()
#curses.echo()
begin_x = 20
begin_y = 7
height = 5
width = 40
win = curses.newwin(height, width, begin_y, begin_x)
tb = curses.textpad.Textbox(win)
text = tb.edit()
curses.addstr(4,1,text.encode('utf_8'))
#hw = "Hello world!"
#while 1:
# c = stdscr.getch()
# if c == ord('p'):
# elif c == ord('q'): break # Exit the while()
# elif c == curses.KEY_HOME: x = y = 0
curses.endwin()
@qguv

This comment has been minimized.

Copy link

commented Aug 7, 2013

This is extremely useful; thank you for posting this!

@myano

This comment has been minimized.

Copy link
Owner Author

commented Nov 5, 2013

@qguv I'm glad this was helpful!

@ritesh

This comment has been minimized.

Copy link

commented Dec 2, 2013

Thank you for the example @qguv For some reason, every time I try any python curses example (both Putty and xterm) the terminal gets corrupted after i exit the program. I can no longer see what I'm typing, and commands like ls produce output without newlines. Do you know what the issue could be? This is python 2.7.3, btw.

@canibanoglu

This comment has been minimized.

Copy link

commented Jan 17, 2014

@riteshk, are you sure that you're calling the curses.endwin() function? Even if you are, if your code somehow exits due to an exception, your terminal will be changed because curses.endwin() will not have been called?

For this reason, it is recommended that you wrap your curses program in curses.wrapper() so that things fail gracefully.

@dldnh

This comment has been minimized.

Copy link

commented Mar 30, 2014

I like it without wrapper() - makes it easier to type into command-line python.

D'oh! why did I think that work work??? :-)

To get out, I just hit ^C and then pasted in the endwin() call

@yesh0907

This comment has been minimized.

Copy link

commented Jul 28, 2015

Thanks so much ;)

@val314159

This comment has been minimized.

Copy link

commented Feb 25, 2016

If your terminal gets completely hosed, you can usually run "tset" which means Terminal reSET.

@reteps

This comment has been minimized.

Copy link

commented Dec 26, 2016

How you can avoid keyboard interrupt problems: catch it, and reset the curses variables.

def reset(screen):
    curses.nocbreak()
    screen.keypad(0)
    curses.echo()
    curses.endwin()
try:
    #your code
    #if it terminates, call `reset(screen)` here too.
except KeyboardInterrupt:
    reset(screen)
    exit()
@ice1x

This comment has been minimized.

Copy link

commented Feb 14, 2018

if c == ord('p'): print hw
?

@hash3liZer

This comment has been minimized.

Copy link

commented Mar 12, 2018

Can someone explains me the usage of curses.wrapper()? It looks pretty simple.

@chrisshroba

This comment has been minimized.

Copy link

commented Apr 10, 2018

Quick tip, if you exit without cleaning up and your terminal is in a wonky state, just run stty sane and it'll be good as new 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.