2017-03-05 168 views
1

我試圖修改ScrollView doc的代碼,使GridLayout中的水平軸上有可滾動的標籤。 GridLayout不應該是可滾動的,只是其中的標籤。Kivy:GridLayout中的水平可滾動標籤

對於我的應用程序,我無法使用kv語言(在.kv或builder中),所以代碼必須在python中。

from kivy.uix.gridlayout import GridLayout 
from kivy.uix.label import Label 
from kivy.uix.scrollview import ScrollView 
from kivy.app import runTouchApp 

layout = GridLayout(cols=1, spacing=10) 

for i in range(10): 
    label = Label(text="abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz", height = 30, size_hint_x=None, width=400) 
    label.bind(minimum_width=label.setter('width')) 
    root = ScrollView(size_hint=(None, 1), size=(label.width, label.height)) 
    root.add_widget(label) 
    layout.add_widget(root) 


runTouchApp(layout) 

回答

1

到目前爲止,這是工作:

import kivy 

from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.button import Button 
from kivy.uix.popup import Popup 
from kivy.uix.label import Label 
from kivy.uix.scrollview import ScrollView 

class Test(App): 
    def build(self): 
     layout = GridLayout (cols=3) 
     for i in range(3): 
      l = Label(font_size=15, size_hint_x= None, width=300, text="This is a very looooooooooooooooooooonnnnnnnnnnnnnnnnnnnng text. Indeed it is a very loooooooooooooooooooooonnnnnnnnnnnnnnnng text") 
      l.bind(size_hint_min_x=l.setter('width')) 
      scroll = ScrollView(size_hint=(None, None), size=(100, 400), pos_hint={'center_x':.5, 'center_y':.5}) 
      scroll.add_widget(l) 

      layout.add_widget(scroll) 
     return layout 


Test().run()