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

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.

主站蜘蛛池模板: 临泉县| 登封市| 克东县| 邯郸县| 华安县| 庆云县| 辰溪县| 高清| 白沙| 巴东县| 苏尼特右旗| 毕节市| 图木舒克市| 阿拉善左旗| 潜山县| 清河县| 柯坪县| 永善县| 九龙县| 佳木斯市| 乌鲁木齐市| 隆子县| 聊城市| 孟村| 恩施市| 九台市| 阿合奇县| 西林县| 兰溪市| 措美县| 台中市| 涟水县| 新田县| 宁蒗| 南陵县| 岚皋县| 云和县| 蓬安县| 襄垣县| 阿瓦提县| 阿拉善盟|