我正在寫一個小程序,需要一個列表,並在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