如何在kivy python中使用滚动条

时间:2017-07-25 15:04:58

标签: python user-interface scrollview kivy

有人可以告诉我如何在此代码中使用滚动条吗?其次,有没有办法对齐标签和TextInput,这样无论输入多少,TextInput中的文本都会清晰可见。这里的对齐意味着:如果有100个(数百或数千个)TextInput,TextInput中的文本应该是正确可见的。实际上,当我在代码中给出一些(间距= 50)时,在输入20秒后,文本显示不正确。 提前致谢。

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.lang import Builder

ROWS = ['Goc', 'COC', 'EEE', 'abs' , 'kju' , 'iop' , 'nmg', 'gty', 'jkio', 'dbkgcd' , 'udbcbjkb']

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        Table:
            padding: 50, 50, 50, 50
            orientation: 'vertical'

<Row>:
    spacing: 50
    size_hint_x: 1
    txt: txtinpt.text
    Label:
        text: root.txt
    TextInput:
        id: txtinpt
        text: root.txt
        disabled: not CheckBox.active
    CheckBox:
        id:CheckBox 
        text: 'CheckBox'
        active: False
    Button:
        text: 'save'

""")
class Table(BoxLayout):
    def __init__(self, **kwargs):
        super(Table, self).__init__(**kwargs)
        for row in ROWS:
            self.add_widget(Row(row))



class Row(BoxLayout):
    txt = StringProperty()
    def __init__(self, row, **kwargs):
        super(Row, self).__init__(**kwargs)
        self.txt = row



class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


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

kivy scrollBar

1 个答案:

答案 0 :(得分:1)

要遵循的步骤是:

  1. ScrollView添加到TabbedPanelItem并将Table放入其中。
  2. 使用Table阻止boxlayout(size_hint_y = None)自动调整其高度到其父窗口小部件的高度。
  3. 指定Row身高。
  4. 代码可以是:

    from kivy.app import App
    from kivy.uix.tabbedpanel import TabbedPanel
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import StringProperty
    from kivy.uix.textinput import TextInput
    from kivy.uix.checkbox import CheckBox
    from kivy.lang import Builder
    
    ROWS = ['Goc', 'COC', 'EEE', 'abs' , 'kju' , 'iop' , 'nmg', 'gty', 'jkio', 'dbkgcd' , 'udbcbjkb']
    
    Builder.load_string("""
    
    <Test>:
        do_default_tab: False
    
        TabbedPanelItem:
            text: 'page1'
    
            ScrollView:
                Table:
                    orientation: "vertical"
                    size_hint_y: None
                    height: self.minimum_height
                    padding: 50, 50, 50, 50
    
    
    <Row>:
        spacing: 50
        size_hint_y: None
        size_hint_x: 1
        height: 100
        txt: txtinpt.text
        Label:
            text: root.txt
    
        TextInput:
            id: txtinpt
            text: root.txt
            disabled: not CheckBox.active
        CheckBox:
            id:CheckBox 
            text: 'CheckBox'
            active: False
        Button:
            text: 'save'
    
    """)
    class Table(BoxLayout):
        def __init__(self, **kwargs):
            super(Table, self).__init__(**kwargs)
            for row in ROWS:
                self.add_widget(Row(row))
    
    
    
    class Row(BoxLayout):
        txt = StringProperty()
        def __init__(self, row, **kwargs):
            super(Row, self).__init__(**kwargs)
            self.txt = row
    
    
    class Test(TabbedPanel):
        pass
    
    class MyApp(App):
    
        def build(self):
            test = Test()
            return test
    
    
    if __name__ == '__main__':
        MyApp().run()
    
      

    注意:修改仅影响kv文件。

    <强>输出: enter image description here