473,238 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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

Sep 15 '07 #1
5 6284
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: J Turner | last post by:
Hello, I'm playing with a python-based shell, which uses (guess what?) python as it's scripting language, for iterative invocation, environment variables, etc. Everything was going well,...
1
by: Edmond Ho | last post by:
Hi, I'm having trouble with a small curses program. I'm associate a pad with a panel. As I understand, a pad is supposed to be just a window with an arbitrary size. That seems to imply that a pad...
1
by: Riccardo Galli | last post by:
Hi, I'm writing some widgets in curses. Actually I'm trying to write a combobox. To do so, I need to create a pad inside a panel, so that I can hide/show it. I can't do it. I can create a...
0
by: Matthew Alton | last post by:
The appended program freaks python 2.2 & 2.3 completely out. To reproduce the wierdness: i) copy the source to a file called consarn.py ii) $ python consarn.py; iii) the program is now doing a...
0
by: Matt Garman | last post by:
I'd like to write a class or module in python that allows me to do on-the-fly color changing in the curses module. I'm thinking about something along the lines of this: addstr(y, x, 'hello',...
4
by: schwerdy | last post by:
Hi together, can someone provide an example of a curses application that works in a xterm that can be resized? I could not find any working example yet... Thanks in advance, Sebastian...
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
3
by: Simon Morgan | last post by:
Hi, I'm having trouble with the following code. The problem is that the value read by getch() when I hit the up or down keys doesn't match curses.KEY_UP or curses.KEY_DOWN respectively. Other...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.