KIVY Python-布局中的滚动视图 [英] KIVY python - scroll view in a layout

查看:173
本文介绍了KIVY Python-布局中的滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,我想在其中插入几个按钮的滚动视图. 所以有基本代码,我想在网格布局内滚动查看. PS:我有此错误:菜单对象没有属性视图"

I have a simple app in which i would like to insert a scroll view of several buttons. So there is the base code, i want a scroll view inside the grid layout. PS: i have this error: Menu object has no attribute 'view'

我想获得什么:

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

推荐答案

Kivy语言

请注意,最外面的小部件将kv规则应用于所有内部小部件 小部件,然后再应用其他任何规则.这意味着如果一个内部 窗口小部件包含ID,这些ID可能在内部 小部件的 init 函数.

Note that the outermost widget applies the kv rules to all its inner widgets before any other rules are applied. This means if an inner widget contains ids, these ids may not be available during the inner widget’s init function.

ScrollView»管理内容大小和位置

默认情况下,size_hint为(1,1),因此内容大小将完全适合您的ScrollView(您没有要滚动的内容).您必须至少停用子级的size_hint指令之一(x或y)以启用滚动.当ScrollView小于最小尺寸时,将size_hint_min设置为None还将启用该尺寸的滚动.

By default, the size_hint is (1, 1), so the content size will fit your ScrollView exactly (you will have nothing to scroll). You must deactivate at least one of the size_hint instructions (x or y) of the child to enable scrolling. Setting size_hint_min to not be None will also enable scrolling for that dimension when the ScrollView is smaller than the minimum size.

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

To scroll a GridLayout on it’s Y-axis/vertically, set the child’s width to that of the ScrollView (size_hint_x=1), and set the size_hint_y property to None:

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

Use Clock.schedule_once to invoke a new method, create_scrollview. Make sure the height is such that there is something to scroll layout.bind(minimum_height=layout.setter('height')). Please refer to the example below for details.

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

输出

这篇关于KIVY Python-布局中的滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆