【问题标题】:flexible number of objects in kivy scrollviewkivy滚动视图中灵活的对象数量
【发布时间】:2015-09-09 01:42:59
【问题描述】:

我正在尝试在一个屏幕上使用输入来使用 kivy 在第二个屏幕上制作列表。基本思想是将段落分解为单词,然后将其显示在另一页上。下面是一个最小的例子。

正在发生的事情是单词被挤压在一起,并且没有滚动。

在 main.py 中:

from kivy.app import App
from kivy.app import Widget

from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


class InputScreen(Screen):
    wordList = ObjectProperty()
    def getwords(self):
        self.wordList=[]
        s = self.ids.textInput.text.split()
        for w in s:
            for punctuation in [',','.','!','?']:
                w.strip(punctuation)

            self.wordList.append(w)

        return None

class WordLabel(Label):
    pass

class WordScreen(Screen):
    wordList = ObjectProperty()
    def updateWords(self):
        self.ids.scrollContainer.clear_widgets()
        wordGrid = GridLayout(cols=2,size_hint_y=None)
        wordGrid.bind(minimum_height=wordGrid.setter('height'))
        for w in self.wordList:
            wordGrid.add_widget(Label(text=w))
            wordGrid.add_widget(Label(text='property of word'))

        self.ids.scrollContainer.add_widget(wordGrid)

class testScreenManager(ScreenManager):
    pass

class testApp(App):
    def build(self):
        sm = testScreenManager()
        return sm


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

在 test.kv 中:

<testScreenManager>
    InputScreen:
        id: inputScreen
        name: 'input'
    WordScreen:
        id: wordScreen
        name: 'word'
        wordList: inputScreen.wordList


<InputScreen>:
    GridLayout:
        cols: 1
        TextInput:
            id: textInput
            hint_text: 'Input paragraph'
        BoxLayout:
            orientation: 'horizontal'
            size_hint_y: .2
            Button:
                text: 'Analyze paragraph'
                on_press: root.getwords()
            Button:
                text: 'See word list'
                on_press:
                    root.getwords()
                    root.manager.transition.direction = 'left'
                    root.manager.current = 'word'
        BoxLayout:
            orientation: 'horizontal'
            size_hint_y: .2
            Label:
                id: wordCountResult
                text: ''


<WordScreen>:
    on_enter: root.updateWords()
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            id: rowLabels
            orientation: 'horizontal'
            size_hint_y: .2
            Label:
                text: 'Words not at grade level:'
                size_hint_x: .5
            Label:
                text: 'Grade Level'
                size_hint_x: .5
        ScrollView:
            id: scrollContainer
            size_hint: (None,None)
            size: (self.parent.width,self.parent.height - rowLabels.height - buttonRow.height)
        Button:
            id: buttonRow
            size_hint_y: .2
            text: 'Back'
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'input'
<WordLabel>:
    size_hint_y: None
    height: '29sp'

我尝试将size_hint_y=.6 用于ScrollView,并在wordGrid.bind(minimum_height=wordGrid.setter('height')) 中交换height 和minimum_height。

我认为会发生的是,ScrollView 容器将是窗口的高度减去其他两个小部件使用的高度。在 ScrollView 内部,WordGrid 较长(高度绑定到所有子项所需的最小高度),因此滚动用于查看所有项目。我一定不了解有关 ScrollView、WordGrid 和 WordGrid 子项的高度/大小的一些事实。有人有什么见解吗?

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    在这部分代码中:

    for w in self.wordList:
            wordGrid.add_widget(Label(text=w))
            wordGrid.add_widget(Label(text='property of word'))
    

    Label 默认为size_hint_y=1。相反,size_hint_y=None 是必需的。可以设置高度,就像在 WordLabel 类中所做的那样 - 这是 OP(由我)打算做的。

    感谢相关问题Kivy ScrollView - Not Scrolling帮助我发现错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2018-03-26
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多