python curses addstr错误 - 但仅限于我的计算机上

sko*_*ite 2 python curses ncurses

当我发现最奇怪的问题(如果你愿意,一个评论很多的副本)时,我正在编写一个小程序,它接受一个列表,并用curses(直接,标准库或其他,电池包括python的curses)生成一个菜单.整个计划如下).简单地说,当接受os.listdir生成列表的结果时,使用addstrERR 诅咒崩溃,但是,如果我将其提供给硬编码列表,则它可以正常工作.当然,这绝对没有意义,对吧?列表是列表是列表,列表中的任何其他名称仍然应该是列表,对吧?

为了使事情变得更复杂,我将代码发送给了我的一个朋友,他主要在python2.6中工作(我最初编写的是在python3.1中工作).他取消注释了broken_input()调用(为程序提供os.listdir生成的信息),并说它对他来说很好.我安装了python 2.6和3.1,所以我改变了我的shebang以使程序在2.6中运行,并且(对于未broken_input()注释的)对我来说,它仍然会抛出addstrERR(但是使用硬编码输入运行良好...这是,当然,顺便说一句,除了概念证明之外完全无用).

因此,我的问题是:我的python安装中是否存在某些问题(我正在运行Ubuntu lucid,安装了python2.6.5和3.1),如果是这样,我该如何修复它以便我可以让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)
Run Code Online (Sandbox Code Playgroud)

Fre*_*Foo 5

你在屏幕上填充太多,从而传递越界线号码addstr.如果您创建一个空目录来运行程序(或放大终端窗口),它就可以工作.

要解决此问题,请在输出循环之前检查窗口中的行数main.