2017-07-20 10 views
-1

私のウィジェットをScrollViewまたはListViewに追加してスクロールするにはどうすればいいですか?私のページをスクロールできません

私は、コードを書いたが、それは良い動作しません、これは私のmain.pyです:

from kivy.app import App 
from kivy.lang import Builder 

from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.scrollview import ScrollView 

from random import random 

class chat_history(BoxLayout): 
def profile(self): 
    return random(),random(),random() 


Builder.load_file('widg.kv') 

class myApp(App): 
def build(self): 
    x=BoxLayout(orientation='vertical') 
    s=ScrollView() 
    for i in range(1,21): 
    x.add_widget(chat_history(height=50)) 
    s.add_widget(x) 
    return s 

myApp().run() 

そして、これは私のKVファイルです:

<chat_history>: 
height:100 
BoxLayout: 
    height:50 
    name:'haha very funny ' 
    size_hint_y:None 
    id:cv 
    orientation:'horizontal' 
    canvas: 
     Color: 
      rgb:root.profile() 
     Ellipse: 
      pos:root.pos 
    Label: 
     text_hint:{'x':0,'y':0.1} 
     pos:root.pos 
     size_hint_x:0.7 
     height:cv.height 
     text:cv.name 
     id:lbl 

答えて

0

まず、レイアウトの高さを親ウィジェットの高さに自動的に適合させないようにする必要があります(size_hint_y = None)。

一方、高さがスクロールするようなものであることを確認してください。明示的にレイアウトの高さを指定する必要があります。

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.scrollview import ScrollView 
from random import random 


kv_text = ''' 
<chat_history>: 
    size_hint_y: None 
    height:100 
    BoxLayout: 
     height:50 
     name:'haha very funny ' 
     size_hint_y:None 
     id:cv 
     orientation:'horizontal' 
     canvas: 
      Color: 
       rgb:root.profile() 
      Ellipse: 
       pos:root.pos 
     Label: 
      text_hint:{'x':0,'y':0.1} 
      pos:root.pos 
      size_hint_x:0.7 
      height:cv.height 
      text:cv.name 
      id:lbl 
''' 

class chat_history(BoxLayout): 
    def profile(self): 
     return random(),random(),random() 

class myApp(App): 
    def build(self): 
     Builder.load_string(kv_text) 
     x=BoxLayout(orientation='vertical', size_hint_y=None) #<<<<<<<<<<< 
     x.bind(minimum_height=x.setter('height'))    #<<<<<<<<<<< 
     s=ScrollView() 
     for i in range(1,21): 
      x.add_widget(chat_history(height=50)) 
     s.add_widget(x) 
     return s 

myApp().run() 
関連する問題