curses: x, y positioning

7

7stud

I can't see to get any y, x coordinates to work with curses. Here is
an example:

import curses

def my_program(screen):
while True:
ch = screen.getch()
if ch == ord("q"):
break
if ch <= 255:
screen.addstr(30, 10, "*%s*" % chr(ch))
screen.refresh()

curses.wrapper(my_program)

Here is the result:

Traceback (most recent call last):
File "2pythontest.py", line 12, 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 9, in my_program
screen.addstr(30, 10, "*%s*" % chr(ch))
_curses.error: addstr() returned ERR
 
T

Tim Roberts

7stud said:
I can't see to get any y, x coordinates to work with curses. Here is
an example:

import curses

def my_program(screen):
while True:
ch = screen.getch()
if ch == ord("q"):
break
if ch <= 255:
screen.addstr(30, 10, "*%s*" % chr(ch))
screen.refresh()

curses.wrapper(my_program)

Don't you want mvaddstr? (And remember that y comes first.)
 
7

7stud

Hi,

Thanks for the response.

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.)
--

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.
 
7

7stud

Hi,

Thanks for the response.



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




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)
 
I

Ian Clark

7stud said:
However, now I am having a problem trying to set the color of the text
that is output:

import curses

def example(screen):
if curses.has_colors():
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)

screen.addstr(1, 1, 'Hello', curses.color_pair(1))
screen.addstr(1, 7, 'World', curses.color_pair(2))
screen.addstr(1, 12, '!!!', curses.color_pair(3) + curses.A_BOLD)
else:
screen.addstr(1, 1, 'You don\'t have colors enabled. :(')

screen.addstr(3, 0, '<Press any key to continue>')
screen.refresh()
screen.getch()

if __name__ == '__main__':
curses.wrapper(example)

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top