格式化文本以适合Python / Curses中的框

时间:2015-06-07 08:40:09

标签: python-3.x python-curses

请告诉别人如何修复诅咒中的文本面板?

我的糟糕结果

----- Panel ------
| Lorem ipsum dol
or sit amet consec
tet uer metus nec 
eu C urabitur elei
fen.             |
|                |
------------------

我想要结果

----- Panel ------
| Lorem ipsum do | 
| lor sit amet   |
| consectet uer  |
| metus nec eu C |
| urabitur       |
| eleifen.       |
------------------

1 个答案:

答案 0 :(得分:0)

试试这个:

from __future__ import division #You don't need this in Python3
from math import *
string = "0123456789012345678901234567890123456789"
columns = 5
rows = int(ceil(len(string)/columns))
for row in range(1,rows+1):
    panel.addstr(row,1,string[(row*columns)-columns:row*columns])

结果是:

01234
56789
01234
56789
01234
56789
01234
56789

以下是python-curses示例:

from __future__ import division  #You don't need this in Python3
import curses
from math import *

screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad( 1 )
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair( 1 )
normalText = curses.A_NORMAL
screen.border( 0 )
curses.curs_set( 0 )
columns = 5
string = "Lorem ipsum dolor sit amet consectetuer metus nec eu C urabitur eleifen."
screen.addstr(1,1,string)
screen.addstr(20,1,"Press ESC to EXIT")
screen.refresh()
rows = int(ceil(len(string)/columns))
box = curses.newwin(rows + 2, columns + 2, 3, 1)
box.box()
for row in range(1,rows+1):
    box.addstr(row,1,string[(row*columns)-columns:(row*columns)])
    box.refresh()

x = screen.getch()
while x != 27:
    x = screen.getch()
curses.endwin()
exit()

Screenshot of the code..