如何使用Kivy加速GUI启动

时间:2019-10-29 17:01:13

标签: python kivy

我正在使用Python Kivy创建GUI。

这里是代码:按下按钮可打开和关闭GUI并显示隐藏的元素。

test.py

from kivy.config import Config
closeWidth = 300
openWidth = 600
height = 300
Config.set('graphics', 'width', closeWidth)
Config.set('graphics', 'height', height)
Config.set('graphics', 'resizable', False)

from kivy.properties import StringProperty,NumericProperty
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.widget import Widget


class TestAppSCN(Widget):
    openOrClose = 0 #0=advansed setting close,1=open
    labelText  = StringProperty()
    nomalWindowSize = NumericProperty()
    advance_setting_opacity = NumericProperty()

    def __init__(self, **kwargs):
        super(TestAppSCN, self).__init__(**kwargs)
        self.labelText = 'Open >>'
        self.nomalWindowSize = 1.0
        self.advance_setting_opacity = 0

    def advanced_setting_buttonClicked(self):
        if self.openOrClose == 0:#closed
            Window.size = (openWidth, height)
            self.openOrClose = 1
            self.nomalWindowSize = 0.5
            self.labelText = "<< Close"
            self.advance_setting_opacity = 1
        else: #opened
            Window.size = (closeWidth, height)
            self.openOrClose = 0
            self.nomalWindowSize = 1.0
            self.labelText = "Open >>"
            self.advance_setting_opacity = 0

class testApp(App):
    def __init__(self, **kwargs):
        super(testApp, self).__init__(**kwargs)
        self.title = 'testApp'

    def build(self):
        return TestAppSCN()

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

test.kv

#:import Label kivy.uix.label.Label
TestAppSCN: # add root

<TestAppSCN>:
    BoxLayout:
        size: root.size

        BoxLayout:
            orientation: 'vertical'
            size_hint: root.nomalWindowSize,1.0
            canvas.before:
                Color:
                    rgba: 0,0,0,0
                Rectangle:
                    pos: self.pos
                    size: self.size

            BoxLayout:
                size_hint : 1.0,1.0
                padding: 5

                Button:
                    size_hint : 1.0,1.0
                    text: root.labelText
                    on_press: root.advanced_setting_buttonClicked()

        #advance setting-------------------------------------------------------
        ScrollView:
            opacity: root.advance_setting_opacity
            size_hint_x: 1.0-root.nomalWindowSize

            BoxLayout:
                orientation: 'vertical'
                size_hint_y: 1.5

                Slider:
                    id: testSlider1
                    size_hint: 1.0,0.3
                    min: 1
                    max: 10
                    value: 10

                Slider:
                    id: testSlider2
                    size_hint: 1.0,0.3
                    min: 1
                    max: 10
                    value: 10

                Slider:
                    id: testSlider3
                    size_hint: 1.0,0.3
                    min: 1
                    max: 10
                    value: 10

                Slider:
                    id: testSlider4
                    size_hint: 1.0,0.3
                    min: 1
                    max: 10
                    value: 10

                Slider:
                    id: testSlider5
                    size_hint: 1.0,0.3
                    min: 1
                    max: 10
                    value: 10

enter image description here

test.pytest.kv是针对问题的简短代码,实际上,我正在编写更长的代码。而且test.kv中的ScrollView非常长,并且使用了100多个元素,因此启动该程序需要很长时间。我花了4秒钟以上的时间来启动程序。编译后的文件开始花费了10秒钟以上。将ScrollView重写为RecycleView并没有更快。

是否可以通过省略隐藏元素的图形来加速GUI启动?

0 个答案:

没有答案