kivy scrollview

时间:2015-09-09 01:43:00

标签: python kivy

我试图在一个屏幕上使用输入来使用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,并在height中交换minimum_heightwordGrid.bind(minimum_height=wordGrid.setter('height'))

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

1 个答案:

答案 0 :(得分:0)

在这部分代码中:

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帮助我实现错误。