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

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.

主站蜘蛛池模板: 灵武市| 晋中市| 怀宁县| 吉木萨尔县| 金沙县| 尉犁县| 阳原县| 广西| 福州市| 陇川县| 岳池县| 天祝| 兰考县| 宜黄县| 左贡县| 祁阳县| 沙坪坝区| 南漳县| 丘北县| 清河县| 白河县| 正宁县| 长泰县| 松阳县| 仙桃市| 聂荣县| 台北县| 建瓯市| 定西市| 苍溪县| 阿合奇县| 隆回县| 凯里市| 康马县| 宁海县| 莱州市| 无极县| 沿河| 略阳县| 白水县| 大埔县|