python - (python) 如何在curses中创建静态文本

标签 python text curses

我正在创建一个文本冒险。我怎样才能添加静态文本呢? 我的意思是一些文本始终保留在窗口的左侧。即使所有其他文本都向下滚动。另外我怎样才能把这个文本变成红色。

最佳答案

这是一个示例,显示一些红色的静态文本(始终位于顶部):

import sys
import curses


curses.initscr()

if not curses.has_colors():
    curses.endwin()
    print "no colors"
    sys.exit()
else:
    curses.start_color()

curses.noecho()    # don't echo the keys on the screen
curses.cbreak()    # don't wait enter for input
curses.curs_set(0) # don't show cursor.

RED_TEXT = 1
curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)

window = curses.newwin(20, 20, 0, 0)
window.box()
staticwin = curses.newwin(5, 10, 1, 1)
staticwin.box()

staticwin.addstr(1, 1, "test", curses.color_pair(RED_TEXT))

cur_x = 10
cur_y = 10
while True:
    window.addch(cur_y, cur_x, '@')
    window.refresh()
    staticwin.box()
    staticwin.refresh()
    inchar = window.getch()
    window.addch(cur_y, cur_x, ' ')
    # W,A,S,D used to move around the @
    if inchar == ord('w'):
        cur_y -= 1
    elif inchar == ord('a'):
        cur_x -= 1
    elif inchar == ord('d'):
        cur_x += 1
    elif inchar == ord('s'):
        cur_y += 1
    elif inchar == ord('q'):
        break
curses.endwin()

结果截图:

enter image description here

请记住,顶部的窗口必须最后进行 refresh() 处理,否则下方的窗口将被绘制在它们之上。

如果您想更改静态文本,请执行以下操作:

staticwin.clear()   #clean the window
staticwin.addstr(1, 1, "insert-text-here", curses.color_pair(RED_TEXT))
staticwin.box()     #re-draw the box
staticwin.refresh()

其中1, 1表示从第二行的第二字符开始写入(记住:坐标从开始) 0)。这是必需的,因为窗口的框是在第一行和第一列上绘制的。

关于python - (python) 如何在curses中创建静态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122268/

相关文章:

python - python多处理中的动态工作池管理

python - "Fatal error in launcher: Unable to create process using"是什么意思?

algorithm - 4 字节散列算法比较小文本(通常小于 2 kb)

text - 从文本生成问题 (NLP)

c - 如何禁用 "curses.h" header (Xcode 6.3 OSX Yosemite 中“stdio.h 的一部分”)以避免函数声明冲突

python - nodelay() 导致 python curses 程序退出

python - Altair 更改绑定(bind) radio 中标签的颜色

Python:将两个 CSV 文件合并为多级 JSON

python - 如何为文本小部件设置字符的最大宽度?

python - curses - addstr 文本在较大的终端中不可见