如何在 Python/Curses 子窗口中滚动文本?
在我使用 Curses 的 Python 脚本中,我有一个子窗口,其中分配了一些文本。由于文本长度可能比窗口大小长,因此文本应该是可滚动的。
Curses 窗口似乎没有任何类似 CSS“溢出”的属性。 Python/Curses 文档在这方面也相当神秘。
这里有人知道如何使用 Python 编写可滚动 Curses 子窗口并实际滚动它吗?
\编辑:更精确的问题
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,
window.scroll
移动窗口的内容太复杂了。相反,curses.newpad 为我做到了。创建一个 pad:
然后您可以根据
cmd
中window.getch()
的输入,通过增加/减少mypad_pos
来滚动:OK with
window.scroll
it was too complicated to move the content of the window. Instead,curses.newpad
did it for me.Create a pad:
Then you can scroll by increasing/decreasing
mypad_pos
depending on the input fromwindow.getch()
incmd
:是的,我对如何使用 pad(为了滚动文本)有点困惑,读完这篇文章后仍然无法弄清楚;特别是因为我想在内容是现有“行数组”的上下文中使用它。所以我准备了一个小例子,它显示了
newpad
和subpad
之间的相似点(和差异):当你运行这个时,首先你会得到类似 (
newpad
左,subpad
右):一些注意事项:
newpad
和subpad
的宽度/高度都应根据内容调整(num行/行数组的最大行宽) + 最终的边框空间scrollok()
允许额外的行 - 但不是额外的宽度scroll()
向上为下一个refresh
方法腾出空间newpad
具有的特殊refresh
方法,然后仅允许此“的一个区域全部内容”将显示在屏幕上;subpad
more-less 必须以其实例化的大小显示─ ──
部分显示在...第 1 行 ────│
部分)。有用的链接:
Right, I was a bit confused on how to utilize pads (in order to scroll text), and still couldn't figure it out after reading this post; especially since I wanted to use it in a context of the content being an existing "array of lines". So I prepared a small example, that shows similarities (and differences) between
newpad
andsubpad
:When you run this, at first you will get something like (
newpad
left,subpad
right):Some notes:
newpad
andsubpad
should have their width/height sized to the content (num lines/max line width of the array of lines) + eventual border spacescrollok()
- but not extra widthscroll()
up to make room for the nextrefresh
method thatnewpad
has, then allows for just a region of this "whole content" to be shown on screen;subpad
more-less has to be shown in the size it was instantiated in───
piece shown at the...Line 1 ───│
part).Useful links:
设置window.scrollok(True)。
文档
Set the window.scrollok(True).
Documentation
我想使用滚动板来显示一些大型文本文件的内容,但这效果不佳,因为文本可能有换行符,并且很难确定一次要显示多少个字符以适应良好的数量。列和行。
因此,我决定首先将文本文件拆分为恰好包含 COLUMNS 个字符的行,当行太短时用空格填充。然后滚动文本变得更加容易。
下面是显示任何文本文件的示例代码:
主要技巧是行:
首先,文本中的制表被转换为空格,然后我使用 splitlines() 方法将文本转换为行数组。
但有些行可能比我们的 COLUMNS 数长,因此我将每一行拆分为 COLUMNS 字符块,然后使用reduce 将结果列表转换为行列表。
最后,我使用 map 用尾随空格填充每行,使其长度恰好为 COLUMNS 个字符。
希望这有帮助。
I wanted to use a scrolling pad to display content of some large text files but this didn't work well because texts can have line breaks and it was pretty hard to figure out how many characters to display at a time to fit the good number of columns and rows.
So I decided to first split my text files in lines of exactly COLUMNS characters, padding with spaces when lines were too short. Then scrolling the text become more easy.
Here is a sample code to display any text file:
The main trick is the line:
First, tabulations in the text are converted to spaces, then I used splitlines() method to convert my text in array of lines.
But some lines may be longer than our COLUMNS number, so I splitted each line in chunk of COLUMNS characters and then used reduce to transform the resulting list in a list of lines.
Finally, I used map to pad each line with trailing spaces so that its length is exactly COLUMNS characters.
Hope this helps.
这是这个问题的答案:
如何在 python-curses 中制作滚动菜单
此代码允许您从字符串列表的框中创建一个小的滚动菜单。
您还可以使用此代码从 sqlite 查询或 csv 文件获取字符串列表。
要编辑菜单的最大行数,您只需编辑
max_row
。如果按 Enter 键,程序将打印选定的字符串值及其位置。
This is the answer of this question:
How to make a scrolling menu in python-curses
This code allows you to create a little scrolling menu in a box from a list of strings.
You can also use this code getting the list of strings from a sqlite query or from a csv file.
To edit the max number of rows of the menu you just have to edit
max_row
.If you press enter the program will print the selected string value and its position.
另一种方法是使用带有切片符号的列表上的 for 循环来打印可见项目。也就是说,您选择要显示的列表的特定部分,然后在每次向上或向下按某个键时添加或减去增量以更改范围。
类似于
list[ y : y +coverage ]
,其中 y 是开始,coverage 确定列表中有多少个项目您想要显示的列表。Another way is to print the visible items using for-loop on list with slice notation. That is, you choose a specific part of the list to display, then add or subtract an increment to change the range every time you press a key up or down.
like
list[ y : y + coverage ]
where y is the start and coverage determines how many items in the list you'd like to display.