诅咒 window.getstr() [英] Curses window.getstr()

查看:21
本文介绍了诅咒 window.getstr()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows XP 上学习 Python 的 curses.我可以让 window.getkey 命令正常工作,但命令 window.getstr 不仅失败,而且程序退出.以下是示例代码行:

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

显然,为了使第一行工作,我正确导入了curses 并执行了stdscr = curses.initscr() 命令.我的窗口已定义并正常工作.我试过将窗口坐标放在 getstr parens 中,并且我使用了 window.move.两者都不起作用.

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.

有什么想法为什么 getstr 不起作用?

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.

这是整个程序:

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

以下是 Traceback 消息的相关部分:

Here's the relevant part of the Traceback message:

window.addstr (8,33,mystr)类型错误:必须是 str,而不是字节.

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

汤姆

推荐答案

回溯告诉您,变量 mystr 是一个字节对象而不是字符串.这意味着您必须先对其进行解码,然后才能将其用作字符串,这是 addstr() 所需要的.

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

这只是一个 Python 3 问题!我也用 Python 2.7 对此进行了测试,无需此更改即可工作.问题是由于 Python 2 和 3 之间的不同字节/字符串处理引起的.我假设您在自己使用 py3 时遵循了 py2 教程.

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.

这篇关于诅咒 window.getstr()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆