当我注意到一个最奇怪的问题时(如果你愿意,一个评论很重的副本整个程序的内容如下)。简而言之,当接受os.listdir
生成列表的结果时,curses会因addstr
错误而崩溃,但是,如果我给它提供一个硬编码列表,它就可以正常工作。当然,这完全没有意义,对吧?列表就是列表,任何其他名称的列表仍然应该是列表,对吧?
为了让事情变得更复杂,我将代码发送给了我的一个主要在 python2.6 中工作的朋友(我的最初是为在 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)