MDLabel 不滚动

MDLabel is not scrolling

这是示例 code.The 标签内的文本不滚动 谁能帮我解决这个问题。

from kivy.uix.scrollview import ScrollView
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen

class Main(MDApp):
    def build(self):
        self.screen=Screen()
        self.s=ScrollView(size_hint=(1,0.1),pos_hint={"center_x":0.5,"center_y":0.5})
        self.l=MDLabel(text="hello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n")
        self.s.add_widget(self.l)
        self.screen.add_widget(self.s)
        return self.screen
        
Main().run()

您需要设置size_hint_y=None,然后将标签的高度设置为实际文字的大小。

from kivy.uix.scrollview import ScrollView
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen


class Main(MDApp):
    def build(self):
        self.screen = Screen()
        self.s = ScrollView(size_hint=(1, 0.1), pos_hint={"center_x": 0.5, "center_y": 0.5})
        self.l = MDLabel(
            size_hint_y=None, # Set size_hint to None
            text="hello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n hello\n hello \n hello \n hello  \nhello\nhello\n"
        )
        self.l.texture_update() # Update the size of label text
        self.l.height = self.l.texture_size[1] # Set the height of the label to the height of the text
        self.s.add_widget(self.l)
        self.screen.add_widget(self.s)
        return self.screen


Main().run()