如何在Python中删除光标窗口并恢复后台窗口?

5 投票
1 回答
10481 浏览
提问于 2025-04-15 21:13

我正在使用Python的curses库,首先我用initscr()创建了一个初始窗口。然后我又创建了几个新的窗口来覆盖这个初始窗口。我想知道是否可以删除这些新窗口,并恢复到标准屏幕,而不需要重新填充内容。有没有什么方法可以做到这一点?有人能告诉我窗口、子窗口、垫子和子垫子之间的区别吗?

我有这段代码:

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.

1 个回答

11

这个例子应该可以正常运行:

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';再按下一个键,它会把窗口去掉,重新显示标准屏幕,这样又会变成全是'S';再按下一个键,脚本就结束了,终端会恢复到正常状态。这样做对你来说不行吗?还是说你其实想要的是别的东西……?

撰写回答