在python中将彩色文本呈现到ncurses窗口

2024-09-21 01:28:19 发布

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

是否可以/如何在python中将预终端格式的彩色文本呈现为curses.window?在

基本上,我尝试在已经呈现彩色日志输出的现有控制台应用程序的基础上添加一些简单的基于curses的UI,我试图将其重定向到curses window。在

然而,似乎诅咒会以某种方式关闭控制颜色的各种控制代码。在

下面是一个最小的脚本,使用colorama作为文本源的代理:

import colorama
import curses

def run_gui():
    stdscr = curses.initscr()
    curses.start_color()
    curses.use_default_colors()

    stdscr.insertln()
    stdscr.insertln()
    stdscr.insstr(colorama.Fore.RED+'wattt'+colorama.Style.RESET_ALL)
    stdscr.insertln()
    stdscr.insstr(colorama.Fore.GREEN+'wattt'+colorama.Style.RESET_ALL)
    stdscr.insertln()
    stdscr.insstr(colorama.Fore.YELLOW+'wattt'+colorama.Style.RESET_ALL)
    stdscr.insertln()
    stdscr.refresh()

if __name__ == '__main__':

    run_gui()

这将呈现:

^{pr2}$

有没有一种方法我可以告诉诅咒允许正常的控制代码,或者我将不得不返工所有现有的颜色功能?在


Tags: 文本style颜色allwindowcursescolorama彩色
1条回答
网友
1楼 · 发布于 2024-09-21 01:28:19

简写:后者(“返工”)。在

长:

“正常控制代码”恰好与设备相关;curses为终端提供了一个与设备无关的接口。引用manual page

The ncurses library routines give the user a terminal-independent method of updating character screens with reasonable optimization.

实际上,大多数curses实现通过terminfo-based functions提供了一个低级接口,它允许您进行特殊调整,但是

  • Python的绑定不包括这一点(它只覆盖ncurses的一部分),并且
  • 对于所建议的那种东西,它是没有用的,因为它是操纵信息,诅咒是为了知道如何比你的程序更有效地管理。阻止它知道屏幕上的内容会干扰诅咒的行为。在

Python文档提到属性,例如在^{}str[^{} attr]^{}

你的例子可以像

stdscr.insstr("wattt", COLOR_PAIR(COLOR_RED))

你问的问题以前也有人问过。例如:

相关问题 更多 >

    热门问题