Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
word wrap text output
#1
my script has huge strings of text to output in a (probably virtual) text-mode environment. i would like to have something that can break up the text in word wrap or other appropriate mode. there a many more things to do than just split by spacing and form what fits. certain hyphenated words can be split at the hyphen, too. maybe there is a module around that does this right. in this case, something new to install on Xubuntu 18.04 is OK since it's just for me. but, a way to do this in the Python Library would still be a plus.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
https://docs.python.org/3/library/textwrap.html
Reply
#3
import pygame
pygame.init()
import textwrap
RED = (200, 20, 20)

screen = pygame.display.set_mode((400, 400))
your_text = """Bunch of text, blah, blah, and soforth.  Maybe a
story or something.  Who knows, doesn't matter"""

my_wrap = textwrap.TextWrapper(width = 25)
wrap_list = my_wrap.wrap(text=your_text)
y = 20
for line in wrap_list:
    myfont = pygame.font.SysFont(None,32) 
    mytext = myfont.render( line, 1, (RED))
    screen.blit(mytext, (0, y))
    y += 20
    
pygame.display.flip()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  easy way to paginate --help text output? Skaperen 1 1,922 Feb-08-2020, 11:34 AM
Last Post: Gribouillis
  libmdbx, prod-ready fork of LMDB: someone can wrap it for python? genryrar 5 5,096 Jun-28-2018, 08:22 PM
Last Post: erthink

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020