你能在 kivy 中创建一个可编辑和可滚动的文本字段吗?

Can you create an editable and scrollable text field in kivy?

问题如标题所示,我正在尝试实现一项功能,允许我的应用程序的用户编辑可编辑文本字段的内容(会有一些 pre-written 东西,但我想要允许用户对其进行编辑)。

我用油漆制作的概念图:

当然,有一些内置的小部件可以帮助您。第一个是 ScrollView(https://kivy.org/doc/stable/api-kivy.uix.scrollview.html) the second is TextInput (https://kivy.org/doc/stable/api-kivy.uix.textinput.html).

我认为你可以将这两者结合起来实现你所建议的那样。

尝试将此添加到您的 kv 文件中:

ScrollView:
    id: scroll_view
    TextInput:
        text: 'Some random text'
        size_hint: 1, None
        height: max(self.minimum_height, scroll_view.height)

请记住,ScrollView 只有在有内容要滚动时才会滚动。要垂直滚动,您应该将 TextInput 的宽度设置为其父 ScrollView 的宽度,高度应设置为 ScrollView 的高度或 TextInput 的高度中较大的一个。

为了完整起见,下面是一些创建可滚动文本字段的示例代码:

from kivy.app import App
from kivy.lang import Builder

kv = Builder.load_string(
"""
ScrollView:
    id: scroll_view
    TextInput:
        text: 'Some random text'
        size_hint: 1, None
        height: max(self.minimum_height, scroll_view.height)
"""
)


class ScrollableTextApp(App):

    def build(self):
        return kv


if __name__ == '__main__':
    ScrollableTextApp().run()