2011-03-21 53 views
2

我正在写一个小程序,需要一个列表,并在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) 
+0

你试图打印查看生成的列表的输出,如果它实际上看起来像一个正常的名单?也许在生成它的函数调用中有一个空条目或其中的东西? – Mikeb 2011-03-21 01:02:46

回答

5

你馅太多到屏幕上,从而通过一个彻头彻尾的越界行号addstr。如果您创建一个空目录来运行程序(或扩大您的终端窗口),它就可以工作。

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

+1

似乎'addstr'可能/应该报告为错误/异常的一部分。 – martineau 2011-03-21 01:27:48

+1

非常感谢所有有用的建议;我害怕我会被撕开或者是...... @larsmans规则:你对金钱绝对正确;非常感谢。如果只有一个半星期前我才知道,我不会试图去调试脚本的每一个可能的方面。这很明显,这是痛苦的,同样亲切的你会花时间帮助像我这样的失败者爱好者。我希望有一天在某个地方有人滑倒你二十块钱。 – skookiesprite 2011-03-21 13:15:45

0

使用screen.scrollok(1)addstr允许文本滚动。

0

问题是在addch手册页解释:

addchwaddchmvaddchmvwaddch程序把字符ch 到指定的窗口在其当前窗口位置,这是先进的 。它们与stdio(3)中的putchar(3)类似。如果 进步是在右边的空白处:

  • ,光标会自动换到下一行的开头。

  • 在当前的滚动区域的底部,并且如果scrollok是 启用,滚动区域滚动一行。

  • 如果没有启用scrollok,右下角 利润率写一个字符都可以成功。但是,在返回一个错误,因为它不是 可以换到一个新行

给定的程序既不从右下缘捕获了一个错误(也许应该说“角”),也呼吁scrollok允许数据向上滚动。在后一种情况下,你将失去它向上滚动,而处理异常将允许显示数据的屏幕的价值后,您提示,然后要么退出或显示更多的数据信息。