使用 python curses 在特定的 y,x 位置更改角色的属性
Change the attributes of a character at a certain y,x location with python curses
假设文本 "hello world" 像这样打印到 curses 屏幕:
stdscr.addstr(0, 0, "hello world")
现在,假设我希望字母 'e'(y,x 位置 1,0)具有属性 A_BOLD
或 A_REVERSE
。我怎么能这样做?是否有一个函数只改变某个 y,x 位置的属性?我正在使用 Python3.6 和 curses(显然)
编辑:我尝试使用 chgat,但它没有正常工作。我是这样用的:
stdscr.chgat(0, 1, curses.A_BOLD)
但这使得 x 位置之后的每个字符都应用了该属性。我只想影响 y,x 位置的字符。
Python curses 包装器有几个 chgat
的变体,具有不同的参数列表。您应该使用的是 window.chgat(
y、x、num, attr)
如果您想返回到给定位置,您可以将当前位置(通过getyx
, and then do a move
获得的位置)保存到保存的位置。
curses.start_color()
curses.init_pair( 1, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair( 2, curses.COLOR_GREEN, curses.COLOR_WHITE)
curses.init_pair( 3, curses.COLOR_YELLOW, curses.COLOR_WHITE)
def pause(t):
# update screen
screen.refresh()
time.sleep(t)
####################
screen.addstr(0, 0, "hello world", curses.color_pair(1))
pause(2)
screen.addstr(0, 1, "e", curses.color_pair(1) | curses.A_BOLD)
pause(2)
screen.addstr(4, 10, "hello world", curses.color_pair(1))
pause(2)
screen.addstr(4, 11, "e", curses.color_pair(2) | curses.A_BOLD)
pause(2)
screen.addstr(8, 20, "hello world", curses.color_pair(3) | curses.A_BOLD | curses.A_UNDERLINE)
pause(2)
screen.addstr(8, 21, "e", curses.color_pair(3))
pause(2)
假设文本 "hello world" 像这样打印到 curses 屏幕:
stdscr.addstr(0, 0, "hello world")
现在,假设我希望字母 'e'(y,x 位置 1,0)具有属性 A_BOLD
或 A_REVERSE
。我怎么能这样做?是否有一个函数只改变某个 y,x 位置的属性?我正在使用 Python3.6 和 curses(显然)
编辑:我尝试使用 chgat,但它没有正常工作。我是这样用的:
stdscr.chgat(0, 1, curses.A_BOLD)
但这使得 x 位置之后的每个字符都应用了该属性。我只想影响 y,x 位置的字符。
Python curses 包装器有几个 chgat
的变体,具有不同的参数列表。您应该使用的是 window.chgat(
y、x、num, attr)
如果您想返回到给定位置,您可以将当前位置(通过getyx
, and then do a move
获得的位置)保存到保存的位置。
curses.start_color()
curses.init_pair( 1, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair( 2, curses.COLOR_GREEN, curses.COLOR_WHITE)
curses.init_pair( 3, curses.COLOR_YELLOW, curses.COLOR_WHITE)
def pause(t):
# update screen
screen.refresh()
time.sleep(t)
####################
screen.addstr(0, 0, "hello world", curses.color_pair(1))
pause(2)
screen.addstr(0, 1, "e", curses.color_pair(1) | curses.A_BOLD)
pause(2)
screen.addstr(4, 10, "hello world", curses.color_pair(1))
pause(2)
screen.addstr(4, 11, "e", curses.color_pair(2) | curses.A_BOLD)
pause(2)
screen.addstr(8, 20, "hello world", curses.color_pair(3) | curses.A_BOLD | curses.A_UNDERLINE)
pause(2)
screen.addstr(8, 21, "e", curses.color_pair(3))
pause(2)