有點晚了,但只是在別人可以使用這個,這是我的解決方案。此示例將動態調整curses文本板窗口小部件的高度。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: Test Scrolling
#
# File test.py
# Author: Dan Huckson
# Date: 20150922
#
import curses
def main(stdscr):
vpwidth = 1
vpheight = 30
string = "A\n BB\n CCC\n DDDD\n EEEEE\n FFFFFF\n GGGGGGG\n HHHHHHHH\n JJJJJJJJJ\n KKKKKKKKKK\n LLLLLLLLLLL\n MMMMMMMMMMMM\n NNNNNNNNNNNNN\n OOOOOOOOOOOOOO END"
height = w = 0
stdscr.refresh()
for c in string:
if c == '\n' and not w: height += 1
else:
if c != '\n':
w += 1
if w < vpwidth: continue
elif w == vpwidth: height += 1
w = 0
height += 1
if w: height += 1
pad = curses.newpad(height+1, vpwidth)
pad.addstr(string)
top = 0
left = 3
offset = key = 0
while True:
if key == curses.KEY_UP and offset: offset -= 1
elif key == curses.KEY_DOWN and (offset + vpheight) < height:
offset += 1
pad.refresh(offset, 0, top, left, vpheight+top, vpwidth+left)
for i in range(vpheight):
stdscr.addstr(i,0, (' %s:' % i)[-3:])
stdscr.addstr(vpheight,0, ' ----~----+----~----+----~----+----~----+----~----+----~----+----~----+----~----')
stdscr.addstr(vpheight+1,0, ' 10 20 30 40 50 60 70 ')
stdscr.addstr(vpheight+2,0, ' ')
stdscr.addstr(vpheight+4,0, '%s %s %s %s %s %s %s %s ' % (offset, 0, 0, 0, vpheight, vpwidth, height, len(string)))
key = stdscr.getch()
curses.wrapper(main)
要計算在給定的Python字符串的換行符'LEN(text.splitlines()'會更容易些。甚至'text.count( '\ n')'。 –
@MartijnPieters但它會給我計算一個文本框中的實際行數,包括包裹的行數? – ilomambo
@MartijnPieters - 你的兩種方法會給出不同的結果 – eumiro