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.
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.
Does anybody here have an idea how I can code a scrollable Curses subwindow using Python and actually scroll through it?
\edit: more precise question
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.
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:
Set the window.scrollok(True).
Documentation
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.
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 from window.getch() in cmd: