Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

curses: x, y positioning

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


bbxx789_05ss at yahoo

Sep 15, 2007, 2:27 PM

Post #1 of 6 (183 views)
Permalink
curses: x, y positioning

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

--
http://mail.python.org/mailman/listinfo/python-list


timr at probo

Sep 16, 2007, 7:41 PM

Post #2 of 6 (162 views)
Permalink
Re: curses: x, y positioning [In reply to]

7stud <bbxx789_05ss [at] yahoo> wrote:

>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, timr [at] probo
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


bbxx789_05ss at yahoo

Sep 17, 2007, 6:21 AM

Post #3 of 6 (158 views)
Permalink
Re: curses: x, y positioning [In reply to]

Hi,

Thanks for the response.

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






--
http://mail.python.org/mailman/listinfo/python-list


bbxx789_05ss at yahoo

Sep 17, 2007, 8:50 AM

Post #4 of 6 (158 views)
Permalink
Re: curses: x, y positioning [In reply to]

On Sep 17, 7:21 am, 7stud <bbxx789_0...@yahoo.com> wrote:
> Hi,
>
> Thanks for the response.
>
> On Sep 16, 8:41 pm, Tim Roberts <t...@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)

--
http://mail.python.org/mailman/listinfo/python-list


bbxx789_05ss at yahoo

Sep 17, 2007, 8:54 AM

Post #5 of 6 (158 views)
Permalink
Re: curses: x, y positioning [In reply to]

On Sep 17, 9:50 am, 7stud <bbxx789_0...@yahoo.com> wrote:
> Ok. This works:
>
> import curses
> import curses.wrapper

Oops. That second import statement isn't necessary.

--
http://mail.python.org/mailman/listinfo/python-list


iclark at mail

Sep 17, 2007, 8:55 AM

Post #6 of 6 (164 views)
Permalink
Re: curses: x, y positioning [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.