我正在尝试为我的 curses 输出添加颜色。然而,挑战在于文本是通过单个长字符串 self.all_results 打印的。有没有办法给字符串的某个部分添加颜色。
def main(self,stdscr):
x,y = 0,0 # size of the window
xx,yy = 50,200 # where to place window - up,across
pad = curses.newpad(150,150) # nlines, ncols
pad_pos = 0
exit = False
pad.addstr(4,0,str(self.all_results))
while not exit:
sleep(0.2)
if self.timer != None:
if time() - start > self.timer:
self.stop = True
break
pad.addstr(0,0,str(self.format_results()))
pad.refresh(pad_pos,0, x,y, xx,yy)
cmd = stdscr.getch()
stdscr.nodelay(1)
if cmd != -1:
pad.refresh(pad_pos,0, x,y, xx,yy)
if len(self.format_results().split('\n')) > 100:
if cmd == curses.KEY_DOWN:
if pad_pos < len(self.format_results())+1:
pad_pos += 1
try:
pad.refresh(pad_pos,0, x,y, xx,yy)
except curses.error:
pass
elif cmd == curses.KEY_UP:
if pad_pos != 0:
pad_pos -= 1
try:
pad.refresh(pad_pos,0, x,y, xx,yy)
except curses.error:
pass
pad.addstr
之前调用pad.attron
和pad.attroff
。 - Ross Ridge