在kivy scrollview中可调整的textinput

时间:2018-05-21 05:06:49

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

我是Kivy的新手。我想在textInput大小适合内容的情况下动态地在Kivy scrollView中正确显示多个textInput。当gridLayout的高度设置为1000时,当前代码只显示3。我想使用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()

1 个答案:

答案 0 :(得分:0)

  1. 将BoxLayout替换为BoxLayout
  2. 设置boxlayout的最小高度,以便滚动。
  3. ScrollView

      

    layout.bind(minimum_height = layout.setter( '高度'))

    实施例

    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
    

    输出

    Img01 - App Startup Img02 - Scrolling