这一点已经在另一个帖子中得到了回答: Wrapping the text of a Kivy Label个
您需要使用text_size
来换行文本.请参阅https://kivy.org/doc/stable/api-kivy.uix.label.html#中的文档
下面是一个使用KV字符串和一个基本应用程序构建的示例.如果运行此命令,您应该会看到文本换行.
text_size: self.width, None
设置宽度以适应应用程序窗口的宽度,不设置高度.
kv_string = '''
BoxLayout:
orientation: 'horizontal'
padding: 10
spacing: 10
Label:
id: content_label
pos_hint: {"center_x":0.5, "center_y":0.5}
font_size: '16'
# generate sample text
text: 'lorem ipsum '*1000
# set text to adapt to the width of the window
text_size: self.width, None
'''
from kivy.lang import Builder
from kivy.app import App
class MainApp(App):
def build(self):
return Builder.load_string(kv_string)
if __name__ == '__main__':
MainApp().run()
但是,这只会水平换行文本,但仍会垂直剪裁文本.如果您希望在全文垂直溢出时能够查看和滚动全文,则需要将标签放入ScrollView
.见https://kivy.org/doc/stable/api-kivy.uix.scrollview.html
在该示例的基础上,下面是生成的换行文本也垂直滚动的情况:
kv_string = '''
BoxLayout:
orientation: 'horizontal'
padding: 10
spacing: 10
ScrollView:
# sets scrolling only vertically
do_scroll_x: False
do_scroll_y: True
Label:
id: content_label
pos_hint: {"center_x":0.5, "center_y":0.5}
font_size: '16'
# resets the height of the Label
size_hint_y: None
# sets the height of the Label to the height of the texture
height: self.texture_size[1]
text: 'lorem ipsum '*1000
text_size: self.width, None
'''
from kivy.lang import Builder
from kivy.app import App
class MainApp(App):
def build(self):
return Builder.load_string(kv_string)
if __name__ == '__main__':
MainApp().run()