0

我在 python 脚本中有这段代码,它工作得很好。我一直在尝试将其转换为 KV,以便将其包含在我的应用程序中,但我的尝试失败了。请帮助 :( 转换所需的代码如下,它已在 python 3.4.4 中编码。目前它被编程为应用程序,但我需要它作为屏幕:

if __name__ == '__main__':
    from kivy.app import App
    from kivy.uix.scrollview import ScrollView
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.button import Button

    class ScrollViewApp(App):

        def build(self):
            layout1 = GridLayout(cols=6, spacing=10, size_hint=(None, None))
            layout1.bind(minimum_height=layout1.setter('height'),
                         minimum_width=layout1.setter('width'))
            for i in range(200):
                btn = Button(text='student'+str(i), size_hint=(None, None),
                             size=(200, 100))
                layout1.add_widget(btn)
            scrollview1 = ScrollView(bar_width='10dp')
            scrollview1.add_widget(layout1)
            root = GridLayout(cols=1)
            root.add_widget(scrollview1)
            return root

    ScrollViewApp().run()
4

1 回答 1

1

我已经添加了更多的东西,所以你可以学到一些新的东西。有问题可以在评论里问我。干得好...

主.py

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.clock import mainthread


class StudentsScreen(Screen):

    grid = ObjectProperty()

    def __init__(self, **kwargs):
        super(StudentsScreen, self).__init__(**kwargs)
        self.create_buttons()

    @mainthread  # slight delay
    def create_buttons(self):
        for i in xrange(200):
            button = StudentButton(text='student %s' % i)
            self.grid.add_widget(button)


class StudentButton(Button):
    pass


class Test(App):
    pass

if __name__ == "__main__":
    Test().run()

测试.kv

ScreenManager:

    StudentsScreen:
        grid: grid

        ScrollView:
            bar_width: '10dp'

            GridLayout:
                id: grid
                size_hint: None, None
                size: self.minimum_width, self.minimum_height
                cols: 6
                spacing: dp(10)


<StudentButton>:
    size_hint: None, None
    size: '200dp', '100dp'
于 2016-04-11T07:36:21.573 回答