当我发现一个最奇怪的问题(如果你愿意的话,下面是整个程序的一个重注释的副本),我正在写一个小程序,该程序会以诅咒的形式从列表中生成一个菜单(直接向上,标准库或其他,电池包括python的诅咒)。简单地说,当接受os.listdir
生成的列表的结果时,curses会崩溃并返回addstr
错误,但是,如果我给它提供一个硬编码的列表,它就可以正常工作了。当然,这完全没有道理,对吧?一个列表就是一个列表,一个以任何其他名称命名的列表应该仍然是一个列表,对吗?在
为了让事情变得更复杂,我把代码发给了我的一个朋友,他主要在python2.6中工作(我的代码最初是为python3.1编写的)。他取消了broken_input()
调用的注释(该调用向程序提供os.listdir
生成的信息),并说这对他来说很好。我已经安装了Python2.6和3.1,所以我修改了shebang,使程序在2.6中运行,并且(取消了broken_input()
的注释),它仍然抛出addstr
错误(但是在硬编码的输入下运行良好。。。这当然是,顺便说一句,除了概念证明之外,完全没用)。在
因此,我的问题是:在我的python安装中是否有问题(我运行的是ubuntulucid,安装了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
中输出循环之前窗口中的行数。在{{{cd2>允许在cd2}之后使用滚动。在
在^{} 手册页中解释了该问题:
给定的程序既不会从右下边距捕获错误(可能应该说是“corner”),也不会调用
scrollok
来允许数据向上滚动。在后一种情况下,您将丢失向上滚动的信息,而处理异常将允许您在显示屏幕上的数据后提示,然后退出或显示更多数据。在相关问题 更多 >
编程相关推荐