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

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.

主站蜘蛛池模板: 同心县| 梅河口市| 金坛市| 醴陵市| 化州市| 长宁区| 哈密市| 新竹市| 吴川市| 桃园市| 蒙城县| 博兴县| 柯坪县| 门源| 冀州市| 塔城市| 新宾| 洛扎县| 和平县| 上栗县| 呼和浩特市| 南通市| 高尔夫| 陕西省| 紫金县| 永城市| 双峰县| 武平县| 乃东县| 岳阳县| 定兴县| 胶南市| 清丰县| 武冈市| 洛南县| 密云县| 阳原县| 米易县| 乐昌市| 天长市| 肇源县|