如何使用 curses 删除行尾的文本

how to delete text to end of line with curses

如何删除到行尾的文本?

stdscr.addstr(5, 5, "Egestas Curabitur Phasellus magnis")

结果屏幕:Egestas Curabitur Phasellus magnis # OK

stdscr.addstr(5, 5, "Elit metus")

结果屏幕:Elit metusrabitur Phasellus magnis#问题

您需要在第二行代码之前调用 stdscr.refresh()。这在 documentation

中有明确说明

All you have to do is to be sure that the screen has been redrawn before pausing to wait for user input, by simply calling stdscr.refresh()

要删除到 EOL(行尾)使用window.clrtoeol():

示例:

import curses

window = curses.initscr()
window.clrtoeol()
window.refresh()

不过,我真的建议在任何 console/TUI 编程中使用伟大的 urwid

更新: Bhargav Rao 是对的;您必须显式调用 window.refresh()

Accordingly, curses requires that you explicitly tell it to redraw windows, using the refresh() method of window objects. In practice, this doesn’t really complicate programming with curses much. Most programs go into a flurry of activity, and then pause waiting for a keypress or some other action on the part of the user. All you have to do is to be sure that the screen has been redrawn before pausing to wait for user input, by simply calling stdscr.refresh() or the refresh() method of some other relevant window.

stdscr.addstr(5, 5, "Elit metus") 之前使用 stdscr.refresh()。它会起作用。