kivy html5,Kivy Basics

Kivy App Life Cycle¶

First off, let’s get familiar with the Kivy app life cycle.

35e14a703974bdbd6f7709dd31e895c1.png

As you can see above, for all intents and purposes, our entry point into our App

is the run() method, and in our case that is “MyApp().run()”. We will get back

to this, but let’s start from the third line:

from kivy.app import App

It’s required that the base Class of your App inherits from the App class.

It’s present in the kivy_installation_dir/kivy/app.py.

Note

Go ahead and open up that file if you want to delve deeper into what the

Kivy App class does. We encourage you to open the code and read through it.

Kivy is based on Python and uses Sphinx for documentation, so the

documentation for each class is in the actual file.

Similarly on line 2:

from kivy.uix.label import Label

One important thing to note here is the way packages/classes are laid out. The

uix module is the section that holds the user interface elements

like layouts and widgets.

Moving on to line 5:

class MyApp(App):

This is where we are defining the Base Class of our Kivy App. You should only

ever need to change the name of your app MyApp in this line.

Further on to line 7:

def build(self):

As highlighted by the image above, show casing the Kivy App Life Cycle, this

is the function where you should initialize and return your Root Widget. This

is what we do on line 8:

return Label(text='Hello world')

Here we initialize a Label with text ‘Hello World’ and return its instance.

This Label will be the Root Widget of this App.

Note

Python uses indentation to denote code blocks, therefore take note that in

the code provided above, at line 9 the class and function definition ends.

Now on to the portion that will make our app run at line 11 and 12:

if __name__ == '__main__':

MyApp().run()

Here the class MyApp is initialized and its run() method called. This

initializes and starts our Kivy application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值