2015-09-09 20 views
1

我想在一個屏幕上使用輸入在第二個屏幕上使用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的滾動型,並在wordGrid.bind(minimum_height=wordGrid.setter('height'))交換heightminimum_height

我認爲會發生的事情是,ScrollView容器將是窗口的高度減去其他兩個小部件使用的高度。在ScrollView中,WordGrid更長(高度綁定到所有孩子所需的最小高度),因此滾動用於查看所有項目。我不能理解有關ScrollView,WordGrid和WordGrid的子項的高度/大小的一些事實。任何人都有一些見解?

回答

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幫助我瞭解錯誤。