Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python curses.newwin not working

I'm learning curses for the first time, and I decided to do it in python because it would be easier than constantly recompiling. However, I've hit a hitch. When I try to update a seccond window, I get no output. Here's a code snippet:


import curses
win = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
field = curses.newwin(1, 20, 1, 1)
field.addstr(0, 0, "Hello, world!", curses.A_REVERSE)
field.refresh()

The normal win window initialized with initscr() works, but the field window doesn't show up. Any help?

Edit: Here's the new, revised code, which still doesn't work.

import curses

ex = None

def main(stdscr):
    global ex
    try:
        curses.curs_set(0)
    except Exception, e:
        ex = e

    field = curses.newwin(25, 25, 6, 6)
    field.border()
    cont = True
    x, y = 0, 0

    while cont:
        stdscr.clear()
        field.clear()
        coords = "%d, %d" % (x, y)
        stdscr.addstr(5, 5, coords, curses.A_REVERSE)
        field.addstr(y+2, x+2, "@", curses.A_BOLD)
        chr = stdscr.getkey()
        if chr == 'h':
            if x > 0: x -= 1
        if chr == 'l':
            if x < 20: x += 1
        if chr == 'j':
            if y > 0: y -= 1
        if chr == 'k':
            if y < 20: y += 1
        if chr == 'q':
            cont = False
            stdscr.clear()
            field.clear()
        stdscr.noutrefresh()
        field.noutrefresh()
        curses.doupdate()

curses.wrapper(main)

if ex is not None:
    print 'got %s (%s)' % (type(ex).__name__, ex)
like image 557
Hussain Avatar asked Jul 03 '10 05:07

Hussain


1 Answers

Seems OK to me -- I always use curses.wrapper and my terminal doesn't support cursor visibility of 0, so this is what I have...:

import curses

ex = None

def main(stdscr):
    global ex
    try:
        curses.curs_set(0)
    except Exception, e:
        ex = e

    field = curses.newwin(1, 20, 1, 1)
    field.addstr(0, 0, "Hello, world!", curses.A_REVERSE) 
    field.refresh()
    field.getch()

curses.wrapper(main)
if ex is not None:
  print 'got %s (%s)' % (type(ex).__name__, ex)

I see the reversed "Hello, world!", then when I hit any key to satisfy the getch the program terminates with the expected msg got error (curs_set() returned ERR).

What are you seeing w/this program...? (Remember the wrapper does initscr and sets noecho and cbreak -- and more importantly resets it when done, which is why I always use it;-).

BTW, I'm using Py 2.6.4 on a Mac (OSx 10.5.8) and Terminal.App. Your platform...?

like image 168
Alex Martelli Avatar answered Sep 16 '22 23:09

Alex Martelli