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

  • Kivy Blueprints
  • Mark Vasilkov
  • 242字
  • 2021-08-06 19:18:46

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.

主站蜘蛛池模板: 康平县| 抚宁县| 滕州市| 台南市| 泗水县| 成都市| 靖江市| 武汉市| 汾西县| 喀什市| 老河口市| 东辽县| 绥滨县| 兖州市| 南部县| 石柱| 焦作市| 新野县| 嵩明县| 南投市| 成武县| 龙陵县| 博乐市| 奉化市| 长海县| 宁河县| 唐山市| 开远市| 玉环县| 阳曲县| 略阳县| 垦利县| 雅江县| 株洲县| 平塘县| 钟山县| 旬邑县| 苍梧县| 冀州市| 佛冈县| 蕉岭县|