Question

I'm trying to learn curses for Python on Windows XP. I can get the window.getkey command to work correctly but the command window.getstr not only fails but the program exits. Here are sample code lines:

x = window.getkey()  # this works
y = window.getstr()  # this fails

Obviously, to get the first line to work I have correctly imported curses and have done the stdscr = curses.initscr() command. My window is defined and working. I've tried placing the window coordinates within the getstr parens and I've used window.move. Neither works.

Any ideas why getstr doesn't work?

Here's more info following the first suggestion:

First, the suggestion to run the program in a command prompt window instead of from the desktop is a good one because the window was indeed disappearing.

Here's the entirety of the program:

# program = testcurses
import curses
stdscr = curses.initscr()

window = stdscr.subwin(23,79,0,0)
window.box()
window.refresh()
window.addstr(2,10, "This is my line of text")
window.addstr(4,20,"What happened? ")
window.refresh()

mykey = window.getkey(5,20)
mystr = window.getstr (6,20)
#window.addstr (7,1, "Key data should be here: ")
#window.addstr (7,33, mykey)
window.addstr (8,1, "Str data should be here: ")
window.addstr (8,33,mystr)
window.refresh()

I remarked the lines pertaining to the display of the key data since that works OK.

Here's the relevant part of the Traceback message:

window.addstr (8,33,mystr) TypeError: must be str, not bytes.

Tom

No correct solution

OTHER TIPS

The traceback tells you, that the variable mystr is a bytes object not a string. This means you have to decode it first, before you can use it as a string, which is needed by addstr().

Here's the change you need to make:

mystr = window.getstr(6,20).decode(encoding="utf-8")

This is a Python 3 problem ONLY! I tested this with Python 2.7 as well, which works without this change. The problem arises due to different bytes/string handling between Python 2 and 3. I assume you followed through a py2 tutorial while using py3 yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top