【问题标题】:Adjustable textinput in kivy scrollviewkivy滚动视图中的可调整文本输入
【发布时间】:2018-05-21 05:06:49
【问题描述】:

我是 Kivy 的新手。我希望在文本输入大小适合内容的地方动态地在 Kivy 滚动视图中正确显示多个文本输入。当前代码仅显示 3,因为 gridLayout 的高度设置为 1000。我想使用 height: self.minimum_height 以便我可以动态添加更多 textInputs 并且它们将正确显示,但似乎无法让它工作.

我的kv文件:

<MyNote@BoxLayout>:
    orientation: 'vertical'

    TextInput:
        height: self.minimum_height
        multiline: True
        text_size: self.size
        size_hint_y: None
        text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'

<MainNotesBoxLayout>
    orientation: 'vertical'

    ScrollView:
        GridLayout:
            cols: 1
            size_hint_y: None

            height: 1000
            #height: self.minimum_height

            MyNote:
            MyNote:
            MyNote:

我的主文件:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

class MainNotesBoxLayout(BoxLayout):
    pass

class NotesApp(App):
    def build(self):
        return MainNotesBoxLayout()

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

【问题讨论】:

  • 我不明白您的问题,您能否提供更好的描述或一些屏幕截图来说明您想要实现什么,以及您目前拥有什么?
  • 当然,我会编辑我的问题。

标签: python-3.x kivy kivy-language


【解决方案1】:
  1. 用 BoxLayout 替换 GridLayout
  2. 设置 boxlayout 的最小高度,以便有东西可以滚动。

ScrollView

layout.bind(minimum_height=layout.setter('height'))

示例

notes.py

​​>
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty


class MyNote(TextInput):
    pass


class MainNotesBoxLayout(BoxLayout):
    container = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(MainNotesBoxLayout, self).__init__(**kwargs)
        self.container.bind(minimum_height=self.container.setter('height'))
        self.add_text_inputs()

    def add_text_inputs(self):
        for x in range(100):
            my_note = MyNote()
            self.container.add_widget(my_note)


class NotesApp(App):
    def build(self):
        return MainNotesBoxLayout()


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

notes.kv

#:kivy 1.10.0

<MyNote>:
    height: self.minimum_height
    multiline: True
    text_size: self.size
    size_hint_y: None
    text: '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15'

<MainNotesBoxLayout>
    container: container
    orientation: 'vertical'

    ScrollView:
        size_hint: (1, 1)
        bar_width: 10
        bar_color: 1, 0, 0, 1   # red
        bar_inactive_color: 0, 0, 1, 1   # blue
        effect_cls: "ScrollEffect"
        scroll_type: ['bars']


        BoxLayout:
            id: container
            orientation: 'vertical'
            size_hint: 1, None

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多