python - 如何使用 ncurses 返回字符串值?

标签 python ncurses curses python-curses

我已经尝试过win.inch(y,x)win.instr(y,x) ,甚至 win.getch()win.getkey() 。没有一个能达到侦探的效果吗?我已经通过 stackoverflow 和 google 进行了搜索,但到目前为止我还没有找到解决方案。我已经阅读了python的curses手册。但还是无法解决这个问题。

我的代码:

import curses

stdscr = curses.initscr()
stdscr.addstr(1,1,'x')
if stdscr.inch(1,1) == 'x':
    stdscr.addstr(2,1,'success')
    stdscr.refresh()

stdscr.getch()
curses.endwin()

我已经运行了这段代码,但屏幕上没有显示“成功”。我昨晚调试了 4 个小时的代码。我确信我被这个值(value)判断断言“if stdscr.inch(1,1,) == 'x':”所困。如何实现我的目的是查看某个坐标处是否有特定的字符串,然后执行相应的操作?

最佳答案

curses.window.inch返回 int,而不是 str

以下行:

if stdscr.inch(1, 1) == 'x':

应该是:

if stdscr.inch(1, 1) & 0xff == ord('x'):

或使用instr(不指定长度,instr返回从给定位置开始到行尾的字符串):

if stdscr.instr(1, 1, 1) == 'x':

关于python - 如何使用 ncurses 返回字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21774333/

相关文章:

user-interface - 创建基于终端的安装程序/向导

python - 如何在 linux 终端上重现类似 wget 的速度界面?

python - 为什么我不能在 python curses 窗口中将 str() 添加到最后一行/最后一行?

python - 诅咒 window.getstr()

python - 尝试使用 send_config_set 时无法在 Netmiko 中进入配置模式

python - 按键分组 tensorflow 数据集,按键分组

python - Tastypie - 未找到嵌套资源字段

c - 如何在格式化字符串上使用 strlen()?

python - 如何暂停 Python 脚本并让用户在 Linux 终端中输入

c++ - 运动不工作 ncurses 游戏