最も奇妙な問題に気づいたとき、リストを取得してそこからcurses(ストレートアップ、標準ライブラリなど、Pythonのcursesを含むバッテリー)でメニューを生成する小さなプログラムを作成していました(必要に応じて、コメントの多いコピープログラム全体は以下)。簡単に言えば、生成されたリストの結果を受け入れるとos.listdir
、curses がaddstr
ERR でクラッシュしますが、ハードコードされたリストをフィードすると、正常に動作します。もちろん、これはまったく意味がありませんよね?リストはリストであり、他の名前のリストはリストであるべきですよね?
事態をさらに複雑にするために、主に python2.6 で作業している友人にコードを送りました (私のコードはもともと python3.1 で動作するように書かれていました)。broken_input()
彼は呼び出し (生成された情報をプログラムに供給する) のコメントを外し、os.listdir
うまく機能したと述べました。Python 2.6 と 3.1 の両方がインストールされているので、シバンを変更してプログラムを 2.6 で実行できるようにしましたが、(broken_input()
コメントを外した状態で) 私にとっては、それでもERR がスローaddstr
されます (ただし、ハードコードされた入力で正常に動作します...つまり、もちろん、ところで、概念実証以外はまったく役に立ちません)。
したがって、私の質問は次のとおりです。Pythonのインストールに何か問題がありますか(python2.6.5と3.1がインストールされた状態でUbuntu lucidを実行しています)、もしそうなら、それを修正して、これを実行するcursesを取得できるようにするにはどうすればよいですか適切にコーディングします。そして、それが私の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)