Python 诅咒 windows 不覆盖字符串

Python Curses for windows not overwriting strings

我使用的包来自:http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses 我正在制作一个这样的屏幕对象:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import curses
import time

class Screen(object):
    def __enter__(self):
        self.stdscr = curses.initscr()
        curses.cbreak()
        curses.noecho()
        self.stdscr.keypad(1)
        curses.curs_set(0)

        Y, X = self.stdscr.getmaxyx()

        gap = 1
        self.bar = self.stdscr.derwin(3, X, 1, 0)
        self.threads = self.stdscr.derwin(Y-4-gap, X/2, 4, 0)
        self.messages = self.stdscr.derwin(Y-4-gap, X/2 -1, 4, X/2 + 1)

        # self.bar.box()
        # self.threads.box()
        # self.messages.box()
        return self

    def __exit__(self, type, value, traceback):
        curses.nocbreak()
        self.stdscr.keypad(0)
        curses.echo()
        curses.endwin()

    def refresh(self):
        self.bar.refresh()
        self.threads.refresh()
        self.messages.refresh()



if __name__ == '__main__':
    with Screen() as S:
        S.bar.addstr(0, 0, "Hello")
        S.refresh()
        time.sleep(2)
        S.bar.addstr(0, 0, "\rHi")
        time.sleep(2)

但是当我实际执行它时它只是保持 "Hello" 而不是覆盖它并打印 "hi"

我试过删除“\r”但无济于事。

有什么想法吗?

如果要清理"Hello",可以在S.bar.addstr(0, 0, "\rHi")前加上S.bar.erase()。并且你还需要在S.bar.addstr(0, 0, "\rHi")之后添加S.refresh()来更新window,否则"Hi"将不会显示。

window.erase()用来清除window。您可以查看文档以获取更多详细信息:https://docs.python.org/2/library/curses.html