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

The starting point

Our "Hello, Kivy" example from the preface is a suitable starting point for this app. We just need to add a layout container, BoxLayout, so that we can fit more than one widget on the screen later.

This is the full source code at this point:

# File: main.py
from kivy.app import App

class ClockApp(App):
    pass

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

# File: clock.kv
BoxLayout:
    orientation: 'vertical'

    Label:
        text: '00:00:00'

Right now, it looks and behaves exactly like the previously seen "Hello, world" app. A BoxLayout container allows two or more child widgets to coexist side by side, stacking either vertically or horizontally. Given just one nested widget, as in the preceding code, BoxLayout fills up all the available screen space with it and thus becomes practically unnoticeable (it's as if Label was a root widget instead, taking over the application window). We will review layouts in more detail later on.

Note

Note that while we may call the main.py file anything we want, the clock.kv file is autoloaded by Kivy, and therefore, has to be named after the application class. For example, if our app class is called FooBarApp, a corresponding .kv file should be named foobar.kv (the class name converted to lowercase and without the -app suffix). Closely following this naming convention allows us to avoid loading Kivy language files manually, which is unequivocally a good thing—less lines of code leading to the same result.

主站蜘蛛池模板: 诏安县| 阳谷县| 保山市| 澜沧| 九江市| 绥棱县| 岢岚县| 滦南县| 贵州省| 萨嘎县| 临高县| 新田县| 虞城县| 库伦旗| 满洲里市| 且末县| 云安县| 呼和浩特市| 崇仁县| 丹寨县| 涞源县| 白城市| 东阿县| 自治县| 新巴尔虎右旗| 江山市| 普安县| 萨嘎县| 禹城市| 上杭县| 昌乐县| 福州市| 丰城市| 德惠市| 鹤岗市| 全椒县| 祁连县| 东丰县| 蚌埠市| 辽源市| 通州区|