如何在Kivy中向滚动视图动态添加项目

时间:2019-03-13 01:50:09

标签: python kivy

你好,我是新手。到目前为止,做基本的事情相对简单,但这让我很困惑。我正在制作一个需要在滚动视图中将矩形画布项目动态添加到网格的应用程序。由于我正在执行此操作,因此需要在python中而不是在.kv文件中创建滚动视图。如何做到这一点,以便在调整窗口大小时矩形的大小与窗口大小相同?

.py文件:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Line,Rectangle
from kivy.uix.carousel import Carousel
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window

class Scroll(ScrollView):
    def __init__(self,  **kwargs):
        super(Scroll, self).__init__(**kwargs)
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter('height'))
        # Make sure the height is such that there is something to scroll.
        for i in range(100):
            SkillStat = RelativeLayout(pos=(0,0), height=100, size_hint_y=None, size_hint_x=self.width)
            with SkillStat.canvas:
                Rectangle(pos=self.pos,size=(self.width, 90))
            layout.add_widget(SkillStat)

        self.add_widget(layout)
        pass
    pass
class Sheet(Carousel):
    pass

class SheetApp(App):
    def build(self):    
        return Sheet()

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

.kv文件:

# file name: Sheet.kv

<Sheet>:
    RelativeLayout:
        Scroll:
            size_hint:(1,1)

1 个答案:

答案 0 :(得分:0)

解决方案

  1. 创建一个继承自RelativeLayout的类。
  2. 使用bind函数更新或删除添加到画布的说明。

摘要

class CustomLayout(RelativeLayout):

    def __init__(self, **kwargs):
        super(CustomLayout, self).__init__(**kwargs)
        with self.canvas:
            self.rect = Rectangle(pos=self.pos, size=(self.width, 90))
        self.bind(pos=self.update_rect, size=self.update_rect)

    def update_rect(self, *args):
        self.rect.pos = self.pos
        self.rect.size = self.size

示例

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Line, Rectangle
from kivy.uix.carousel import Carousel
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.lang import Builder


class CustomLayout(RelativeLayout):

    def __init__(self, **kwargs):
        super(CustomLayout, self).__init__(**kwargs)
        with self.canvas:
            self.rect = Rectangle(pos=self.pos, size=(self.width, 90))
        self.bind(pos=self.update_rect, size=self.update_rect)

    def update_rect(self, *args):
        self.rect.pos = self.pos
        self.rect.size = self.size


class Scroll(ScrollView):
    def __init__(self, **kwargs):
        super(Scroll, self).__init__(**kwargs)
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter('height'))
        # Make sure the height is such that there is something to scroll.
        for i in range(100):
            SkillStat = CustomLayout(pos=(0, 0), height=100, size_hint_y=None, size_hint_x=self.width)
            layout.add_widget(SkillStat)

        self.add_widget(layout)


class Sheet(Carousel):
    pass


Builder.load_file('main.kv')


class SheetApp(App):
    def build(self):
        return Sheet()


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

main.kv

#:kivy 1.11.0

<Sheet>:
    RelativeLayout:
        Scroll:
            size_hint:(1,1)
            bar_width: 10
            effect_cls: "ScrollEffect"
            scroll_type: ['bars']
            bar_color: [1, 0, 0, 1]     # red color
            bar_inactive_color: [0, 0, 1, 1]    # blue color

输出

Scrollview - App Start Up Scrolling RelativeLaouts Window resized - smaller Window resized - bigger