python诅咒addstr错误-但仅在我的计算机上

python诅咒addstr错误-但仅在我的计算机上,python,ncurses,curses,Python,Ncurses,Curses,我正在写一个小程序,它可以直接在curses-up、标准库或其他任何地方生成一个菜单,电池包括python的curses,当我注意到一个最奇怪的问题时,如果你愿意的话,下面是整个程序的一个有大量注释的副本。简单地说,当接受os.listdir生成的列表的结果时,curses会因addstr错误而崩溃,但是,如果我给它提供了一个硬编码列表,它就可以正常工作。这当然毫无意义,对吧?一个列表就是一个列表就是一个列表,任何其他名字的列表都应该是一个列表,对吗 为了使事情更加复杂,我将代码发送给我的一位朋

我正在写一个小程序,它可以直接在curses-up、标准库或其他任何地方生成一个菜单,电池包括python的curses,当我注意到一个最奇怪的问题时,如果你愿意的话,下面是整个程序的一个有大量注释的副本。简单地说,当接受os.listdir生成的列表的结果时,curses会因addstr错误而崩溃,但是,如果我给它提供了一个硬编码列表,它就可以正常工作。这当然毫无意义,对吧?一个列表就是一个列表就是一个列表,任何其他名字的列表都应该是一个列表,对吗

为了使事情更加复杂,我将代码发送给我的一位朋友,他主要在python2.6中工作。我最初是用python3.1编写的。他取消了中断的输入调用的注释,该调用将os.listdir生成的信息提供给程序,并说它对他来说运行良好。我已经安装了Python2.6和3.1,所以我更改了shebang,使程序在2.6中运行,并且在断开的_输入没有注释的情况下,它仍然抛出addstr ERR,但在硬编码输入下运行良好。。。当然,顺便说一句,除了概念证明之外,这是完全无用的

因此,我的问题是:在我运行Ubuntu lucid的python安装中,安装了python2.6.5和3.1,是否存在一些问题?如果是,我如何修复它,以便让诅咒正确执行此代码。而且,如果不是我的python安装,我如何从诅咒中获得相同的功能,即:从包含任意数量项的列表中绘制菜单,对它们进行编号,以便用户可以根据项编号进行选择

#!/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)

您在屏幕上填充的内容太多,因此将越界行号传递给addstr。如果您创建一个空目录来运行程序,或者放大终端窗口,它就会工作


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

在addstr之后使用screen.scrollok1以允许文本滚动

手册页中解释了该问题:

addch、waddch、mvaddch和mvwaddch例程将字符放入ch 进入给定窗口的当前窗口位置,然后 先进的它们类似于stdio3中的putchar3。如果 预付款处于正确的边际:

光标将自动换行到下一行的开头

在当前滚动区域的底部,如果scrollok 启用后,滚动区域向上滚动一行

如果未启用,则在右下角写入字符 保证金成功。但是,返回一个错误,因为它不是 可以换行到新行


给定的程序既不捕获右下角的错误,也不调用scrollok以允许数据向上滚动。在后一种情况下,您将丢失向上滚动的信息,而处理异常将允许您在显示屏幕上的数据后进行提示,然后退出或显示更多数据。

您是否尝试打印生成列表的输出,以查看它实际上是否与正常列表类似?可能生成它的函数调用中有一个空条目或某些内容?似乎addstr可以/应该报告为错误/异常的一部分。非常感谢所有有用的建议;我担心我会被撕成碎片之类的@拉斯曼规则:你在金钱上是绝对正确的;非常感谢你。如果我在一个半星期前就知道了,我就不会在调试脚本的每一个方面时变得近乎疯狂。很明显,你抽出时间来帮助像我这样的失败者是件痛苦的事,你也同样仁慈。我希望有一天,某个地方,有人偷偷给你二十美元。