我在 ScrollView 中有一个标签,希望当(多行)文本占用太多行时,用户可以上下滚动,当行太长时,用户可以左右滚动。而且我无法让它滚动。这是我的 Builder 字符串的相关部分:
ScrollView:
id: scrollLayout
size_hint_y: 0.9
Label:
id: sortFilesDisplay
size_hint_y: 0.9
text: 'Drag\\n in\\n files\\n to\\n be\\n sorted\\n yea\\n go\\n now\\n testing\\n please\\n work\\n now\\help'
我读过 Kivy docs on ScrollView ,也就是说我需要指定 size_hints 之一来启用滚动。我已经阅读了一些关于让 ScrollView 工作的 SO 帖子:
- Kivy ScrollView - Not Scrolling
- kivy scrollview is not working
- kivy Scrollview can't Scroll
- ScrollView widget not scolling in kivy
- Kivy -- scroll view not working. And and how add chat users on side pane
- Kivy - My ScrollView doesn't scroll
但它们都涉及嵌入某种布局(例如 GridLayout),但我没有嵌入布局,只是嵌入一个标签。我已尝试将 minimum_height 设置为我在这些帖子中看到的各种内容,但仍然没有效果。
那两个 size_hint_y 只是为了按照指示去做;我不需要它们。还尝试将它们设置为无。
有什么想法吗?
另外,文本的静态字符串并不是我最终想要的。我希望标签在字符串更改时保持可滚动(当用户放入新的文件列表时),但我认为“动态”操作可能是个问题,所以现在我“撤退”以尝试静态字符串。
请您参考如下方法:
为了使 Label 可以滚动,它应该大于 ScrollView 小部件,因此您需要将 size_hint 设置为 无并将标签大小绑定(bind)到文本大小。例如:
测试.kv:
ScrollView:
Label:
id: sortFilesDisplay
size_hint: None, None # <<<<<<<<<<
size: self.texture_size # <<<<<<<<<<
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" * 20
main.kv
from kivy.app import App
from kivy.config import Config
Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '200')
class TestApp(App):
pass
if __name__ == '__main__':
TestApp().run()
