By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

curses: x, y positioning

P: n/a
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

Sep 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
7stud <bb**********@yahoo.comwrote:
>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.)
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 17 '07 #2

P: n/a
Hi,

Thanks for the response.

On Sep 16, 8:41 pm, Tim Roberts <t...@probo.comwrote:
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.


Sep 17 '07 #3

P: n/a
On Sep 17, 7:21 am, 7stud <bbxx789_0...@yahoo.comwrote:
Hi,

Thanks for the response.

On Sep 16, 8:41 pm, Tim Roberts <t...@probo.comwrote:
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)

Sep 17 '07 #4

P: n/a
On Sep 17, 9:50 am, 7stud <bbxx789_0...@yahoo.comwrote:
Ok. This works:

import curses
import curses.wrapper
Oops. That second import statement isn't necessary.

Sep 17 '07 #5

P: n/a
7stud wrote:
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

Sep 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.