gpt4 book ai didi

python curses addstr 错误 - 但只在我的电脑上

转载 作者:行者123 更新时间:2023-11-28 22:05:25 24 4
gpt4 key购买 nike

当我注意到最奇怪的问题(如果你愿意,a整个程序的大量评论副本如下)。简而言之,当接受 os.listdir 生成的列表的结果时,curses 崩溃并出现 addstr 错误,但是,如果我给它提供一个硬编码列表,它工作正常。当然,这完全没有意义,对吧?列表就是列表就是列表,任何其他名称的列表都应该仍然是列表,对吧?

为了使事情变得更加复杂,我将代码发送给了我的一个主要在 python2.6 中工作的 friend (我的代码最初是为在 python3.1 中工作而编写的)。他取消注释 broken_input() 调用(它为程序提供 os.listdir 生成的信息)并说它对他来说工作得很好。我同时安装了 python 2.6 和 3.1,所以我更改了 shebang 以使程序在 2.6 中运行,并且(broken_input() 未注释)对我来说,它仍然抛出 addstr ERR(但在硬编码输入下运行良好......当然,顺便说一句,除了概念证明之外完全没用)。

因此,我的问题是:我的 python 安装是否有问题(我正在运行 Ubuntu lucid,安装了 python2.6.5 和 3.1),如果是,我该如何修复它以便得到诅咒正确执行此代码。而且,如果这不是我的 python 安装,我怎样才能从 curses 中获得相同的功能(即:从包含任意数量项目的列表中绘制菜单,对它们进行编号,以便用户可以根据项目编号进行选择).

#!/usr/bin/env python3.1
"""curses_mp3eater.py: a curses-based implementation of my mp3eater program;
diplays the contents of cwd, allows user to make a selection. But I'm having
problems getting it to iterate over a list.
v0.1 03.14.11
by skookie sprite
address@gmail.com
"""

import curses, curses.wrapper, os, sys


def working_input():
"""the following is demo code to demonstrate my problem... main will accept the following,
but won't accept the product of a directorylist for reasons that I can't figure out."""
dircontents=['this','is','a','list','','and','it','will','iterate','fine','in','the','(main) function.']
return dircontents

def broken_input():
"""this is the code that I NEED to have work... but for reasons beyond me will not iterate in
the main function. It's a simple list of the contents of the CWD."""
cwd=os.getcwd()
dircontents=[]
for item in os.listdir(cwd):
dircontents += [item]
return dircontents

def main(stdscr):
"""This is the program. Designed to take a list of stuff and display it. If I can solve
that hurdle, I'll add selection mechanisms, and break it across screens - amongst other
things. But, currently, it can only accept the demo code. Uncomment one or the other to
see what I mean."""
#broken_input returns an addstr() ERR, but I don't see the difference between working_input
#and broken_input as they are both just lists.
#working_input() is demo code that illustrates my problem
stuffin=working_input()
#stuffin=broken_input()

#the rest of this stuff works. The problem is with the input. Why?
linenumber=int()
linenumber=6
itemnumber=int()
itemnumber=1

stdscr.clear()
stdscr.border(0)

for item in stuffin:
stdscr.addstr(linenumber, 10, '%s - %s' % (itemnumber, item), curses.A_NORMAL)
linenumber += 1
itemnumber += 1

curses.doupdate()
stdscr.getch()



if __name__ == '__main__':
curses.wrapper(main)

最佳答案

您在屏幕上填充了太多内容,因此将越界行号传递给 addstr。如果你创建一个空目录来运行程序(或放大你的终端窗口),它就可以工作。

要解决此问题,请在 main 中的输出循环之前检查窗口中的行数。

关于python curses addstr 错误 - 但只在我的电脑上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5372809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com