【问题标题】:How to print a new line or automatically print new line when the text's length is over the windows size in Python?当文本的长度超过 Python 中的窗口大小时,如何打印新行或自动打印新行?
【发布时间】:2019-08-06 05:27:48
【问题描述】:

当文本的长度超过窗口宽度或超过我限制的长度时,我想打印到新行。 "\n" 在这里不起作用,因为我正在使用一个函数来塑造文本消息。

示例:

input_message = "Hello, world!"
message = text_format(input_message, font, 90, yellow)

# Text Renderer
def text_format(message, textFont, textSize, textColor):
    newFont=pygame.font.Font(textFont, textSize)
    newText=newFont.render(message, 0, textColor)
    return newText

我希望 input_message 会打印如下:

“你好,”

“世界!” (“世界!”超过窗口宽度)。

如何修改text_format函数来实现?

【问题讨论】:

    标签: python css python-3.x python-2.7 pygame


    【解决方案1】:

    你可以使用CSS属性text-overflow

    text-overflow 属性指定如何将未显示的溢出内容通知给用户。它可以被剪裁、显示省略号 (...) 或显示自定义字符串。

    .container {
      white-space: normal; 
      border: 1px solid #000000;
    }
    <div class="container">The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.</div>

    【讨论】:

    • white-space: wrap 不是有效值。此外,您的 text-overflow: clip 在这里什么也不做...这是默认值
    • 没有。它是一种 CSS 样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多