本文介绍了格式化文本以使其适合Python/Curses中的框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告知某人如何在诅咒中修复文本面板?

Please advise someone how to fix a text panel in 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.       |
------------------

推荐答案

尝试一下:

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()

这篇关于格式化文本以使其适合Python/Curses中的框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:22