我正在写一个小程序,需要一个列表,并在curses中生成一个菜单(直线上升,标准库或任何,电池包括python的诅咒),当我注意到最奇怪的问题(如果你愿意,整个程序的重要评论拷贝如下)。简单地说,接受os.listdir
生成列表的结果时,curses与addstr
ERR崩溃,但是,如果我给它一个硬编码列表,它工作正常。当然,这绝对没有意义,对吧?列表是一个列表是一个列表,并且任何其他名字的列表仍然应该是一个列表,对吗?蟒蛇curses addstr错误 - 但只在我的电脑上
为了使事情变得更加复杂,我将代码发送给主要在python2.6中工作的我的一位朋友(我的原本是在python3.1中编写的)。他取消了对broken_input()
呼叫的注释(它为节目生成的信息提供os.listdir
),并表示它对他而言效果不错。我安装了python 2.6和3.1,所以我改变了我的shebang以使程序运行在2.6,并且(对于broken_input()
uncommented)对我来说,它仍然抛出了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
[email protected]
"""
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)
你试图打印查看生成的列表的输出,如果它实际上看起来像一个正常的名单?也许在生成它的函数调用中有一个空条目或其中的东西? – Mikeb 2011-03-21 01:02:46