python 诅咒 addstr 错误 - 但只在我的电脑上 [英] python curses addstr error - but only on my computer

查看:64
本文介绍了python 诅咒 addstr 错误 - 但只在我的电脑上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我注意到一个最奇怪的问题时(如果你愿意,我正在写一个小程序,它接受一个列表并在curses中生成一个菜单(直接,标准库或其他,电池包括python的curses)整个程序的大量评论副本如下).简而言之,当接受 os.listdir 生成列表的结果时,curses 会因 addstr 错误而崩溃,但是,如果我给它提供一个硬编码列表,它就可以正常工作.当然,这完全没有意义,对吧?一个列表就是一个列表,任何其他名称的列表应该仍然是一个列表,对吧?

I was writing a little program that takes a list and generates a menu out of it in curses (straight up, standard library or whatever, batteries included python's curses) when I noticed the strangest problem (if you'd like, a heavily commented copy of the entire program is below). Simply put, when accepting the results of an os.listdir generated list, curses crashes with an addstr ERR, BUT, if I feed it a hardcoded list, it works fine. This, of course, makes absolutely no sense, right? A list is a list is a list and a list by any other name should still be a list, right?

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

To make things even more complicated, I sent the code to a friend of mine who works mainly in python2.6 (mine was originally written to work in python3.1). He uncommented the broken_input() call (which feeds the program the os.listdir generated information) and said that it worked fine for him. I have both python 2.6 and 3.1 installed, so I changed my shebang to make the program run in 2.6, and (with the broken_input() uncommented) for me, it still throws the addstr ERR (yet runs fine with the hardcoded input... which is, of course, btw, entirely useless apart from proof of concept).

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

Thus, my question is this: is there something broken in my python installation (I'm running Ubuntu lucid, with python2.6.5 and 3.1 installed), and, if so, how do I fix it so I can get curses to execute this code properly. And, if it's not my python installation, how can I get the same functionality out of curses (i.e.: paint a menu from a list containing an arbitrary number of items, numbering them so that the user can make a selection based on the item number).

#!/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.如果你创建一个空目录来运行程序(或放大你的终端窗口),它就可以工作.

You're stuffing too much onto the screen and thus passing an out-of-bounds line number to addstr. If you make an empty directory to run the program in (or enlarge your terminal window), it works.

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

To fix this, check the number of lines in the window before the output loop in main.

这篇关于python 诅咒 addstr 错误 - 但只在我的电脑上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆