简体   繁体   English

如何调整小部件的大小,使其大小适合 kivy 中的文本

[英]How to resize a widget so it size is fit to its text in kivy

When I add a widget, namely Label , sometimes its size cannot fit it's text height.当我添加一个小部件时,即Label ,有时它的大小不适合它的文本高度。 Changing its height manually doesn't helped because i can type a text so its height will exceed the label's height.手动更改它的高度没有帮助,因为我可以键入文本,因此它的高度将超过标签的高度。 Here is the example of the code.py file这是code.py文件的例子

#testing.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
import random
import GlobalVariable
from GlobalVariable import *
import time

class ScreenOne(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("testing.kv")

class testing(App):
    def build(self):
        return kv

if __name__ == "__main__":
    testing().run()

.kv file .kv 文件

#testing.kv
WindowManager:
    ScreenOne:

<ScreenOne>:
    ScrollView:
        size: self.size
        GridLayout:
            size_hint_y: None
            size: root.height, root.width
            cols: 2
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            Label:
                text: "boom\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboom"
            

When you run the code, you will see that there is text that exceed it's label height.运行代码时,您会看到有超过 label 高度的文本。 I expect to make those label always fit the text (not shorter but also not longer than its text) whatever the text is, and is scrollable.我希望使那些 label 始终适合文本(不短但也不长于其文本),无论文本是什么,并且是可滚动的。 Any help or suggestion is appreciated.任何帮助或建议表示赞赏。 Thanks.谢谢。

Use these properties for your Label将这些属性用于您的Label

Label:
    text_size: self.width, None
    size_hint: 1, None
    height: self.texture_size[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM