2011-03-21 57 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允許數據向上滾動。在後一種情況下,你將失去它向上滾動,而處理異常將允許顯示數據的屏幕的價值後,您提示,然後要麼退出或顯示更多的數據信息。