问题
Please advise someone how to fix a text panel in curses?
My bad result
----- Panel ------
| Lorem ipsum dol
or sit amet consec
tet uer metus nec
eu C urabitur elei
fen. |
| |
------------------
I want the result
----- Panel ------
| Lorem ipsum do |
| lor sit amet |
| consectet uer |
| metus nec eu C |
| urabitur |
| eleifen. |
------------------
回答1:
Try with this:
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])
The result of this is:
01234
56789
01234
56789
01234
56789
01234
56789
Here is a python-curses
example:
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()
来源:https://stackoverflow.com/questions/30691617/formatting-text-to-fit-within-a-box-in-python-curses