Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python curses wrapper

2,457 views
Skip to first unread message

Mag Gam

unread,
Dec 31, 2011, 2:24:37 PM12/31/11
to pytho...@python.org
Hello,

I have been struggling reseting the terminal when I try to do
KeyboardInterrupt exception therefore I read the documentation for
curses.wrapper and it seems to take care of it for me,
http://docs.python.org/library/curses.html#curses.wrapper.

Can someone please provide a Hello World example with python curses wrapper?

tia

Alexander Kapps

unread,
Dec 31, 2011, 2:34:49 PM12/31/11
to pytho...@python.org
Use atexit.register() to register a cleanup function which is called
when the program exits:

import atexit
import curses

def cleanup():
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()

atexit.register(cleanup)

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)

curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)

stdscr.bkgd(curses.color_pair(1))
stdscr.refresh()

win = curses.newwin(5, 20, 5, 5)
win.bkgd(curses.color_pair(2))
win.box()
win.addstr(2, 2, "Hallo, Welt!")
win.refresh()

c = stdscr.getch()

Alexander Kapps

unread,
Dec 31, 2011, 2:49:55 PM12/31/11
to pytho...@python.org
On 31.12.2011 20:34, Alexander Kapps wrote:
> On 31.12.2011 20:24, Mag Gam wrote:
>> Hello,
>>
>> I have been struggling reseting the terminal when I try to do
>> KeyboardInterrupt exception therefore I read the documentation for
>> curses.wrapper and it seems to take care of it for me,
>> http://docs.python.org/library/curses.html#curses.wrapper.
>>
>> Can someone please provide a Hello World example with python
>> curses wrapper?
>>
>> tia
>
> Use atexit.register() to register a cleanup function which is called
> when the program exits:

Oh, sorry, I missed the curses.wrapper() part. Here is an example:


import curses

def main(screen):
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)

screen.bkgd(curses.color_pair(1))
screen.refresh()

win = curses.newwin(5, 20, 5, 5)
win.bkgd(curses.color_pair(2))
win.box()
win.addstr(2, 2, "Hallo, Welt!")
win.refresh()

c = screen.getch()

try:
curses.wrapper(main)
except KeyboardInterrupt:
print "Got KeyboardInterrupt exception. Exiting..."
exit()

Mag Gam

unread,
Dec 31, 2011, 2:52:10 PM12/31/11
to Alexander Kapps, pytho...@python.org
thanks for your prompt reply. Why would I have to use atexeit?
According to the documentation, curses.wrapper should handle what
cleanup() should be doing.

Neverthless, good to know it exists :p


On Sat, Dec 31, 2011 at 2:34 PM, Alexander Kapps <alex....@web.de> wrote:
> On 31.12.2011 20:24, Mag Gam wrote:
>>
>> Hello,
>>
>> I have been struggling reseting the terminal when I try to do
>> KeyboardInterrupt exception therefore I read the documentation for
>> curses.wrapper and it seems to take care of it for me,
>> http://docs.python.org/library/curses.html#curses.wrapper.
>>
>> Can someone please provide a Hello World example with python curses
>> wrapper?
>>
>> tia
>
>
> Use atexit.register() to register a cleanup function which is called when
> the program exits:
>
> import atexit
> import curses
>
> def cleanup():
>    curses.nocbreak()
>    stdscr.keypad(0)
>    curses.echo()
>    curses.endwin()
>
> atexit.register(cleanup)
>
> stdscr = curses.initscr()
> curses.noecho()
> curses.cbreak()
> stdscr.keypad(1)
>
> curses.start_color()
> curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE)
> curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
>
> stdscr.bkgd(curses.color_pair(1))
> stdscr.refresh()
>
> win = curses.newwin(5, 20, 5, 5)
> win.bkgd(curses.color_pair(2))
> win.box()
> win.addstr(2, 2, "Hallo, Welt!")
> win.refresh()
>
> c = stdscr.getch()
>
> --
> http://mail.python.org/mailman/listinfo/python-list
0 new messages