【问题标题】:how to use scrollbar in kivy python如何在 kivy python 中使用滚动条
【发布时间】:2017-12-31 14:32:05
【问题描述】:

谁能告诉我如何在这段代码中使用滚动条?其次,是否有任何方法可以对齐标签和 TextInput,以便 TextInput 中的文本无论有多少 Input 都将清晰可见。这里对齐意味着:如果有 100s(数百或数千)个 TextInputs ,则 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()

【问题讨论】:

  • 添加滚动视图很简单,但我不确定您所说的“对齐标签和 TextInput”是什么意思,您能举个例子或图片来说明您想要什么吗?
  • 亲爱的@FJSevilla,会有成百上千行,所以我们应该通过滚动看到输入。

标签: python user-interface scrollview kivy


【解决方案1】:

要遵循的步骤是:

  1. ScrollView 添加到TabbedPanelItem 并将Table 放入其中。
  2. 防止 boxlayout (Table) 使用 size_hint_y = None 自动调整其高度以适应其父窗口小部件的高度。
  3. 指定Row 高度。

代码可能是:

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文件。

输出:

【讨论】:

  • 对于成百上千的行,你想使用 RecycleView,而不是创建所有的小部件,否则你的软件肯定会慢得让人无法接受。
猜你喜欢
  • 2018-01-04
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2020-01-22
  • 2014-12-28
  • 2017-02-24
  • 2014-05-05
相关资源
最近更新 更多