『Python Kivy』API说明:kivy.app.App

App类是建立Kivy应用的基础。咱们能够将其当作是Kivy运行循环当中的主入口。在绝大多数的例子中,你建立这个类的子类,而后构建你本身的应用。当你已经准备好开始应用的整个生命周期时,你能够实例化你定制的app类,而后调用这个实例的run()方法。html

建立一个应用

重载build()方法

为了使用一个widget树初始化你的应用,你须要重载build()方法,并返回你已经构建的widget树。python

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout

class IndexScreen(GridLayout):
    def __init__(self, **kwargs):
        super(IndexScreen, self).__init__(**kwargs)
        pass

# 这里不能直接使用App做为你本身建立的应用类的类名
class TestApp(App):
    def build(self):
        return IndexScreen()

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

使用kv文件建立应用

你也可使用kivy语言建立应用。.kv文件能够同时包含规则以及根widget定义。json

#:kivy 1.8.0

Button:
    size: root.size
    text: "Hello ,World"
import kivy
kivy.require('1.8.0')

from kivy.app import App

class MyApp(App):
    pass

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

应用配置

使用配置文件

Kivy支持为你的应用建立独立的配置文件,以下所示:api

class MyApp(App):
    def build_config(self, config):
        config.setdefaults('section1', {
            'key1': 'value1',
            'key2': 'value2'
            })

系统将根据build_config方法中所提供的信息,自动建立名为my.ini的文件,并设置对应的配置信息。app

[section1]
key2 = value2
key1 = value1

你也能够不使用build_config方法,而是直接建立my.ini文件,应用在运行的时候会自动加载这一文件。须要注意的是,配置文件的名字须要与你的应用类的前缀相匹配。性能

示例:应用设置面板

你能够扩展App.build_settings()方法建立你本身的设置面板。你能够参考settings找到具体建立的方法。ui

class MyApp(App):
    def build_settings(self, settings):
        jsondata = """
            [
                { "type": "title",
                  "title": "Test application" },

                { "type": "options",
                  "title": "My first key",
                  "desc": "Description of my first key",
                  "section": "section1",
                  "key": "key1",
                  "options": ["value1", "value2", "another value"] },

                { "type": "numeric",
                  "title": "My second key",
                  "desc": "Description of my second key",
                  "section": "section1",
                  "key": "key2" }
            ]
        """
        settings.add_json_panel('Test application', self.config, data=jsondata)

当你运行起来后,可使用F1来启动设置面板。使用JSON的设置选项,将与以前建立的配置文件的内容相关联。你也能够在程序中调用App.open_settings()App.close_settings()来开启或关闭你的设置面板。你在设置面板中的设置,将会自动保存到你的配置文件中。默认的,kivy的全局设置也会被加载到设置面板中,你可使用以下声明关闭它:设计

class MyApp(App):
    use_kivy_settings = False
    # ...

你还能够手动的调整设置面板中的项目,具体参见settingscode

使用on_start 与 on_stop检测应用性能

参见cProfilehtm

import cProfile

class MyApp(App):
    def on_start(self):
        self.profile = cProfile.Profile()
        self.profile.enable()

    def on_stop(self):
        self.profile.disable()
        self.profile.dump_stats('myapp.profile')

定制设置界面

你能够经过设置App.settings_cls来选择多种不一样的设置界面样式,你还能够建立本身的设置界面。具体参考kivy.uix.settings

你能够经过重载App.display_settings()来定制设置面板的显示,这个方法将会在设置面板在屏幕上显示以前被调用。以下所示:

def display_settings(self, settings):
    try:
        p = self.settings_popup
    except AttributeError:
        self.settings_popup = Popup(content=settings,
                                    title='Settings',
                                    size_hint=(0.8, 0.8))
        p = self.settings_popup
    if p.content is not settings:
        p.content = settings
    p.open()

def close_settings(self, *args):
    try:
        p = self.settings_popup
        p.dismiss()
    except AttributeError:
        pass # Settings popup doesn't exist

最后,若是你想要替换目前的设置面板,你使用App.destroy_settings()能够移除到设置面板的内部索引。若是你已经更改App.display_settings(),你应该当心探测设置面板是否已经被替换。

暂停模式

这个模式是尝试性的,被设计用来针对手机与平板。可能会引发你的应用崩溃或者暂停。

在手机或者平板上,用户可能在任意的时候切换到另外一个应用当中,这个时候,默认的,kivy会激活App.on_stop()事件。

若是你提供暂停模式,当切换到另外一个应用的时候,你的应用将无限等待,直到用户切换回来。在Android上,有一个OpenGL的问题:当你暂停的时候,OpenGL ES Context不能确保被存储。存储OpenGL数据的机制并无在Kivy中实现。

下面是一个暂停模式的示例:

class TestApp(App):

   def on_pause(self):
      # 你能够在这个存储数据
      return True

   def on_resume(self):
      # 在这里你能够检查是否有须要替换的数据
      pass

参考

相关文章
相关标签/搜索