官术网_书友最值得收藏!

Hello, Kivy

When learning a new programming language or technology, the first thing demonstrated to students is traditionally a "hello, world" program. This is how it looks in Python:

print('hello, world')

The Kivy version of a "hello, world" is a little lengthier and consists of two files, namely, a Python module and a .kv layout definition.

Code

A Kivy application's entry point is customarily called main.py, and its contents are as follows:

from kivy.app import App

class HelloApp(App):
    pass

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

As you can see, this takes Kivy's App class, adds absolutely nothing to it, and calls run().

Layout

A layout file is typically named after the application class, in this case HelloApp, sans the App suffix and in lowercase: hello.kv. It contains the following lines:

Label:
    text: 'Hello, Kivy'

This is a very simple Kivy layout definition consisting of a single widget, Label, with the desired text inside. Layout files allow building complex widget hierarchies in a concise, declarative fashion, which is clearly not displayed here, but will be heavily used over the course of this book.

If we run the program now (refer to the Installing and running Kivy section for details), this is what we'll get:

Layout

Our first application powered by Kivy

Now you're ready to move on to the first chapter and start writing real programs.

主站蜘蛛池模板: 河曲县| 晋州市| 芜湖市| 惠来县| 灌云县| 泾川县| 钟祥市| 汉沽区| 辉县市| 香港 | 上思县| 阿拉善盟| 大竹县| 武强县| 霞浦县| 武穴市| 宁南县| 三都| 徐州市| 莱州市| 保康县| 巴东县| 蓬莱市| 霍林郭勒市| 永春县| 平原县| 东乌珠穆沁旗| 揭东县| 景泰县| 大石桥市| 三台县| 安多县| 江油市| 常熟市| 中阳县| 昔阳县| 延长县| 莒南县| 额济纳旗| 彝良县| 瑞金市|