app: add app classe for building application or test

This commit is contained in:
Mathieu Virbel 2010-11-04 12:25:04 -04:00
parent 5cb726bd55
commit d3a1dacefb
1 changed files with 56 additions and 0 deletions

56
kivy/app.py Normal file
View File

@ -0,0 +1,56 @@
'''
App: base class for creating a Kivy application
Example of very simple application with button ::
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def init(self):
self.root = Button(label='hello world'))
MyApp().run()
'''
from kivy.base import EventLoop, runTouchApp
from kivy.event import EventDispatcher
class App(EventDispatcher):
def __init__(self, **kwargs):
super(App, self).__init__()
self.register_event_type('on_start')
self.register_event_type('on_stop')
self.use_default_uxl = kwargs.get('use_default_uxl', True)
self.is_build = False
self.root = None
def build(self):
'''Initialization of the application, will be called only once.
If the build() return a widget, it will be used as the root widget.
'''
pass
def run(self):
'''Launch the app in standalone mode
'''
if not self.is_build:
self.root = self.build()
self.dispatch('on_start')
if self.root:
runTouchApp(self.root)
else:
runTouchApp()
self.dispatch('on_stop')
def on_start(self):
'''Event called after initialization, and before running the
application
'''
pass
def on_stop(self):
'''Event called after running, before closing the application
'''
pass