首页 > 解决方案 > 当文本的长度超过 Python 中的窗口大小时,如何打印新行或自动打印新行?

问题描述

当文本的长度超过窗口宽度或超过我限制的长度时,我想打印到新行。"\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函数来实现呢?

标签: pythoncsspython-3.xpython-2.7pygame

解决方案


您可以使用 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>


推荐阅读