Kivy:GridLayout中的水平可滚动标签

时间:2017-03-05 13:27:04

标签: python scroll kivy

我正在尝试调整ScrollView doc中的代码,以便在GridLayout内的水平轴上获得可滚动标签。 GridLayout不应该可滚动它,只能滚动它。

对于我的应用程序,我不能使用kv语言(在.kv或构建器中),因此代码必须在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 个答案:

答案 0 :(得分: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()