【问题标题】:Convert Python script to KV language as a screen将 Python 脚本转换为 KV 语言作为屏幕
【发布时间】:2016-04-11 05:28:51
【问题描述】:

我在 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()

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    我添加了更多内容,因此您可以学习新知识。有问题可以在cmets问我。给你...

    main.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()
    

    test.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'
    

    【讨论】:

    • 嘿,谢谢你的帮助,我改变了滚动屏幕的大小,所以我在屏幕顶部有一点空间,我打算用作菜单栏,任何想法关于我该怎么做? @jilgeza
    • @Abul 您可以使用 ActionBar 小部件。我看到文档页面已关闭,但如果您转到目录 kivy/uix 并运行 actionbar.py,您将看到一个演示。
    • 非常感谢,不过我还有另一个问题,每次我从屏幕返回并重新进入(在同一个主循环中)时,它都会生成另一组这些按钮。我怎样才能让它不那样做?
    • @Abul 我已经编辑了代码。将on_enter 更改为init
    • @Abul 如果足够,请将答案标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2021-04-15
    相关资源
    最近更新 更多