Sorry...hit shift enter to send by accident before being done...lets try again:
I'm not sure this is the simplest way to explain, but this is what is actually goes on under the hood:lets look at what the App class (https://github.com/kivy/kivy/blob/master/kivy/app.py) actually does when we call run() on an instance to start the our itapp.py lines 559 - 570:def run(self):
'''Launches the app in standalone mode.
'''
if not self.built:
self.load_config()
self.load_kv()
root = self.build()
if root:
self.root = root
if self.root:
from kivy.core.window import Window
Window.add_widget(self.root)
#[...]
so if build() hasn't been called manually already, the app will load it's config, call load_kv(), and finally call self.build and set the root widget to teh return value of self.build() (but only if it actually returned something!)
lets check what load_kv and build do by default to figure out whats happening:
load_kv in in app.py on line 342:
def load_kv(self):
# [...] removed a bunch of code to get thge right file name
# and make sure its actually there etc.
root = Builder.load_file(filename)
if root:
self.root = root
return True
so load_kv calls Builder.load_file() with the apps kv file. Builder.load_file() parses a text file and registers all the KV rules so they are applied to any widgets you create from then on. If there is a root widget in the kv file, Builder.laod_file() will actually instantiate and construct that widget and return it as the return value of Builder.load_file. Notice how you can actually use Builder.load_file to create an instance of a widget at any time (e.g. you can have a seperate kv file for more complicated widgets that have many subwidgets and binding, and then just create an instance by loading that kv file instead of writing a bunch of python code for hooking them all together).
so at this point, if there is a actual widget instance defined in the kv file, it will be the apps root widget.
ok, now lets just check App.build() on line 306:
def build(self):
if not self.root:
return Widget()
so actually, if is already a root widget from the kv file....this does nothing. However, alot of times, we will use the build method to controll exactly how our app is constructed. When we want to construct the root widget and do other related setup in python code, we will overwrite the build method and have it return a widget to use as the apps root widget.
In the pong tutorial, there is actually no widget instance in the kv file, but if you look at PongApp class, you can see how it overwrites the build() method to create a root instance (of type PongApp), start the game, schedules the update function on the clock, and then returns the game widget to be the root widget of the App.
class PongApp(App):
def build(self):
game = PongGame()
game.serve_ball()
Clock.schedule_interval(game.update, 1.0 / 60.0)
return game
The <PongGame> section in the kv file that you mention, is not a widget instance, but a rule that defines how to construct all instances of PongGame (so you dont have to write all the hookup code in python in the PongGame widget class)
hope that helps :)
--
Thomas
On Friday, April 5, 2013 2:22:28 AM UTC-5, BobB wrote: