2010-04-04 168 views
2

地獄 - 夥計們,我正在蟒蛇詛咒,我有我的初始窗口與initscr(),我創建了幾個新的窗口重疊它,我想知道如果我可以刪除這些窗口和恢復標準屏幕沒有必須重新填充它。有沒有辦法?我也可以問,如果有人能告訴我窗口,子窗口,墊和子墊之間的區別。如何刪除python中的curse窗口並恢復後臺窗口?

我有這樣的代碼:

stdscr = curses.initscr() 
####Then I fill it with random letters 
stdscr.refresh() 
newwin=curses.newwin(10,20,5,5) 
newwin.touchwin() 
newwin.refresh() 

####I want to delete newwin here so that if I write stdscr.refresh() newwin won't appear 

stdscr.touchwin() 
stdscr.refresh() 

####And here it should appear as if no window was created. 

回答

7

此,e.g,應該工作:

import curses 

def fillwin(w, c): 
    y, x = w.getmaxyx() 
    s = c * (x - 1) 
    for l in range(y): 
     w.addstr(l, 0, s) 

def main(stdscr): 
    fillwin(stdscr, 'S') 
    stdscr.refresh() 
    stdscr.getch() 

    newwin=curses.newwin(10,20,5,5) 
    fillwin(newwin, 'w') 
    newwin.touchwin() 
    newwin.refresh() 
    newwin.getch() 
    del newwin 

    stdscr.touchwin() 
    stdscr.refresh() 
    stdscr.getch() 

curses.wrapper(main) 

此填充終端用 'S';在任何按鍵時,它都會用'w'填滿窗口。在下一次擊鍵時,它會刪除窗口並再次顯示stdscr,所以它又是全部的''';在下一次擊鍵時,腳本結束並且終端恢復正常。這不適合你嗎?或者你真的想要不同的東西......?

+0

嘿!感謝:D,以及我真正需要的唯一的事情是德「德爾」我不知道如何刪除窗口,它在我的代碼裏面工作,真的非常感謝。 – 2010-04-06 03:30:47

+0

我徘徊 - 可以使用'newwin = None',而不是'del newwin'(所以可以測試'newwin',而不會引發'UnboundLocalError'或類似的情況)?也就是說,如果只指定'newwin = None',Python垃圾收集器是否會正確處理'newwin'資源? – sdaau 2013-05-13 07:15:54

+0

對於所有的意圖和目的,「del newwin」在功能上等同於「newwin = None」。也就是說,「del」實際上並不刪除* newwin;它只是清理那個特定的參考,將其參考計數減1。 – rdb 2014-01-13 15:59:34