如何在 Python/Curses 子窗口中滚动文本? [英] How to scroll text in Python/Curses subwindow?
问题描述
在我使用 Curses 的 Python 脚本中,我有一个 subwin,其中分配了一些文本.因为文本长度可能比窗口大小长,所以文本应该是可滚动的.
In my Python script which uses Curses, I have a subwin to which some text is assigned. Because the text length may be longer than the window size, the text should be scrollable.
Curses 窗口似乎没有任何 CSS-溢出"之类的属性.Python/Curses 文档在这方面也相当含糊.
It doesn't seem that there is any CSS-"overflow" like attribute for Curses windows. The Python/Curses docs are also rather cryptic on this aspect.
这里有人知道如何使用 Python 编写可滚动的 Curses 子窗口并实际滚动它吗?
Does anybody here have an idea how I can code a scrollable Curses subwindow using Python and actually scroll through it?
\edit: 更精确的问题
\edit: more precise question
推荐答案
OK with window.scroll 移动窗口的内容太复杂了.相反,curses.newpad 为我做了.
OK with window.scroll it was too complicated to move the content of the window. Instead, curses.newpad did it for me.
创建一个垫子:
mypad = curses.newpad(40,60)
mypad_pos = 0
mypad.refresh(mypad_pos, 0, 5, 5, 10, 60)
然后您可以根据 cmd 中 window.getch() 的输入通过增加/减少 mypad_pos 来滚动:
Then you can scroll by increasing/decreasing mypad_pos depending on the input from window.getch() in cmd:
if cmd == curses.KEY_DOWN:
mypad_pos += 1
mypad.refresh(mypad_pos, 0, 5, 5, 10, 60)
elif cmd == curses.KEY_UP:
mypad_pos -= 1
mypad.refresh(mypad_pos, 0, 5, 5, 10, 60)
这篇关于如何在 Python/Curses 子窗口中滚动文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!