curses: x, y positioning

7stud bbxx789_05ss at yahoo.com
Mon Sep 17 11:50:52 EDT 2007


On Sep 17, 7:21 am, 7stud <bbxx789_0... at yahoo.com> wrote:
> Hi,
>
> Thanks for the response.
>
> On Sep 16, 8:41 pm, Tim Roberts <t... at probo.com> wrote:
>
> > Don't you want mvaddstr?
>
> import curses
>
> def my_program(screen):
>     while True:
>         ch = screen.getch()
>
>         if ch == ord("q"):
>             break
>         if ch <= 255:
>             screen.mvaddstr(30, 10, "*%s*" % chr(ch))
>             screen.refresh()
>
> curses.wrapper(my_program)
>
> Traceback (most recent call last):
>   File "2pythontest.py", line 13, in ?
>     curses.wrapper(my_program)
>   File "/Library/Frameworks/Python.framework/Versions/2.4//lib/
> python2.4/curses/wrapper.py", line 44, in wrapper
>     return func(stdscr, *args, **kwds)
>   File "2pythontest.py", line 10, in my_program
>     screen.mvaddstr(5, 5, "*%s*" % chr(ch))
> AttributeError: mvaddstr
>
>
>
> >(And remember that y comes first.)
> > --
> >>I can't seem to get any y, x coordinates to work with curses.
>
> However, while changing things around, I discovered what is causing
> the error:  the coordinate 30, 10 is off my screen.  When I change the
> y, x coordinates to 5, 10, then the program executes as it should.
>
> However, now I am having a problem trying to set the color of the text
> that is output:
>
> import curses
>
> def my_program(screen):
>     while True:
>         ch = screen.getch()
>
>         if ch == ord("q"):
>             break
>         if ch <= 255:
>             output = "*%s*" % chr(ch)
>
>             screen.addstr(5, 5, output, curses.COLOR_RED)
>             screen.refresh()
>
> curses.wrapper(my_program)
>
> A strange thing is happening.  The integer value of the constant
> curses.COLOR_RED is 1, and when I type in a character, 1 is getting
> added to the character's ascii code.  For instance, if I type in an
> 'h', then an 'i' displays on the screen--and in white, not red.
>
> I did some testing and has_colors() returns True, but I still can't
> get the curses output to show up in red.  The tutorial:
>
> Curses Programming with Python
> A.M. Kuchling, Eric S. Raymond
>
> says:
>
> To use color, you must call the start_color() function soon after
> calling initscr(), to initialize the default color set (the
> curses.wrapper.wrapper() function does this automatically). Once
> that's done, the has_colors() function returns TRUE if the terminal in
> use can actually display color.
>
> Another thing that is strange: that paragraph uses the syntax
> curses.wrapper.wrapper().  And the docs say this:
>
> --------
> 6.17 curses.wrapper -- Terminal handler for curses programs
> New in version 1.6.
>
> This module supplies one function, wrapper()...
> --------
>
> which implies that I should be using the syntax
> curses.wrapper.wrapper() in my code.  But I get an error when I try
> it:
>
> Traceback (most recent call last):
>   File "2pythontest.py", line 16, in ?
>     curses.wrapper.wrapper(my_program)
> AttributeError: 'function' object has no attribute 'wrapper'
>
> I get the same error if add the import statement:
>
> import curses.wrapper
>
> I'm using an intel mac if that makes any difference.

Ok.  This works:

import curses
import curses.wrapper

def my_program(screen):
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)

    while True:
        ch = screen.getch()

        if ch == ord("q"):
            break

	if ch <= 255:
            output = "*%s*" % chr(ch)

            screen.addstr(5, 5, output, curses.color_pair(1))
            screen.refresh()

curses.wrapper(my_program)




More information about the Python-list mailing list