how to use scrollbar in kivy python

2019-07-27 23:21发布

Can anybody tell me how to use scrollbar in this code? And secondly, is there any way to align the labels and TextInput so that text inside the TextInput will be visible clearly no matter how much Input will be there. Here alignment means: if there is 100s(hundreds or thousands) of TextInputs , the text inside TextInput should be properly visible. Actually when I was giving some (spacing = 50) in the code, after some 20s input, the text was not visible properly. Thanks in advance.

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条回答
何必那么认真
2楼-- · 2019-07-27 23:36

The steps to follow are:

  1. Add a ScrollView to TabbedPanelItem and put Table inside it.
  2. Prevent the boxlayout (Table) from automatically adapting its height to the height of its parent widget using size_hint_y = None.
  3. Specify Row height.

The code could be:

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()

Note: The modifications only affect kv file.

Output: enter image description here

查看更多
登录 后发表回答