[英]How to adjust the size of current widget class when its size based on another widget class in Kivy
[英]How to resize a widget so it size is fit to its text in kivy
當我添加一個小部件時,即Label
,有時它的大小不適合它的文本高度。 手動更改它的高度沒有幫助,因為我可以鍵入文本,因此它的高度將超過標簽的高度。 這是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 文件
#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"
運行代碼時,您會看到有超過 label 高度的文本。 我希望使那些 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.