KIVY python - 在布局中滚动视图

时间:2017-10-14 21:26:25

标签: scrollview kivy

我有一个简单的应用程序,我想插入几个按钮的滚动视图。 所以有基本代码,我想要一个网格布局内的滚动视图。 PS:我有这个错误:菜单对象没有属性' view'

我想获得什么: enter image description here

debug.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button


class AppScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(AppScreenManager, self).__init__(**kwargs)


class Menu(Screen):

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        base = ["element {}".format(i) for i in range(40)]

        for element in base:
            self.view.add_widget(Button(text=element, size=(40,40), size_hint=(1, None), background_color=(0.5, 0.5, 0.5, 1), color=(1,1,1,1)))


Builder.load_file("debug.kv")

class MyAppli(App):

    def build(self):
        return AppScreenManager()

if __name__ == '__main__':
    MyAppli().run()

debug.kv:

#:kivy 1.9.1

<AppScreenManager>:
    Menu:

<Menu>:

    BoxLayout:
        orientation: 'vertical'

        BoxLayout:
            size: (64, 64)
            size_hint: (1, None)

            Button:
                text: "Menu"
                color: (1,1,1,1)
                background_color: (.3, .3, .3, 1)

        GridLayout: # here i want a scrollview
            id: view
            cols: 1

2 个答案:

答案 0 :(得分:0)

ScrollView:
    size: self.size
    GridLayout: # here i want a scrollview
        id: view
        cols: 1
        size_hint_y: None
        height: self.minimum_height
        Button:
            text:
            size_hint_y: None

做这样的事情并添加gridlayout中所需的按钮......我是Kivy的新手,所以请耐心等待。

答案 1 :(得分:0)

Kivy Language

  

请注意,最外面的小部件将kv规则应用于其内部   应用任何其他规则之前的小部件。这意味着如果内心   widget包含id,这些id在内部可能不可用   小部件的 init 功能。

ScrollView » Managing the Content Size and Position

  

默认情况下,size_hint为(1,1),因此内容大小将完全适合您的ScrollView(您将无需滚动)。您必须至少停用子项的size_hint指令(x或y)中的一个才能启用滚动。当ScrollView小于最小大小时,将size_hint_min设置为None也将启用该维度的滚动。

     

要在其Y轴/垂直方向上滚动GridLayout,请设置子项   宽度为ScrollView(size_hint_x = 1)的宽度,并设置   size_hint_y属性为无:

使用 Clock.schedule_once 调用新方法create_scrollview。确保高度可以滚动layout.bind(minimum_height=layout.setter('height'))。有关详细信息,请参阅以下示例。

实施例

debug.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.clock import Clock


class AppScreenManager(ScreenManager):

    def __init__(self, **kwargs):
        super(AppScreenManager, self).__init__(**kwargs)


class Menu(Screen):
    view = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        Clock.schedule_once(self.create_scrollview)

    def create_scrollview(self, dt):
        base = ["element {}".format(i) for i in range(40)]
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter("height"))

        for element in base:
            layout.add_widget(Button(text=element, size=(50, 50), size_hint=(1, None),
                                     background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1)))
        scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        scrollview.add_widget(layout)
        self.view.add_widget(scrollview)


Builder.load_file("debug.kv")


class MyAppli(App):

    def build(self):
        return AppScreenManager()


if __name__ == '__main__':
    MyAppli().run()

debug.kv

#:kivy 1.10.0

<AppScreenManager>:
    Menu:

<Menu>:
    view: view

    BoxLayout:
        orientation: 'vertical'

        BoxLayout:
            size: (64, 64)
            size_hint: (1, None)

            Button:
                text: "Menu"
                color: (1, 1, 1, 1)
                background_color: (.3, .3, .3, 1)

        ScrollView:
            id: view

输出

Scrollview of buttons