如何在ncurses屏幕中移动字符串?

2024-10-01 00:33:40 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我在第一行有一个字符串“Hello world”。 我怎样才能把它移到第二行呢

ps:我知道我可以使用这样的代码:

import curses

stdscr = initscr()
stdscr.adstr(x,y,"Hello World")
y += 1
stdscr.erase()
stdscr.adstr(x,y,"Hello World")
stdscr.getch()

但是在这个“Hello World”下我有很多上下文,我想找到一种只移动或删除“Hello World”的方法。我该怎么做?在


Tags: 方法字符串代码importhelloworldcursesps
2条回答

使用^{}

import curses
import curses.panel

x = 3
y = 3
stdscr = curses.initscr()


w = curses.newwin(1, 20, y, x)
p = curses.panel.new_panel(w)
w.addstr(0, 0, "Hello World")
w.getch()

p.move(y+1, x)
curses.panel.update_panels()
curses.doupdate()
w.getch()

curses.endwin()

顺便说一句,addstr的参数是y, x, str,而不是{}。在

Demo

如果文本只有一行,则可以使用^{}函数从光标开始删除整行:

curses.setsyx(y, 0)   # beginning of the correct line
# stdscr.move(y, 0)   # alternative
stdscr.clrtoeol()    # clears the current line

或者,您可以使用^{}函数插入一整行空白:

^{pr2}$

相关问题 更多 >