Search code examples
pythonkivykivy-language

kivy app runs without implementing the build() method


Was wondering why the Kivy code kept on showing me the same black window despite doing some updates on the kv file. Then noticed I had a typo on the buidl() method.

From the docs "...implementing its build() method so it returns a Widget instance (the root of your widget tree) ...", you have to implement the method.

Why does this code run and give the default black window?

# game.py
from kivy.app import App
from kivy.uix.widget import Widget


class Game(Widget):
    pass


class GameApp(App):
    def buidl(self):
        return Game()


GameApp().run()

The kv file

#game.kv
<Game>:
    canvas:
        Color:
            rgb: .5,.5, 1.0
        Rectangle:
            pos: 0,0
            size: self.size

Running kivy 1.11.1 python 3.7


Solution

  • Kivy apps have a default build() method, which you can see here; it just returns an empty widget. Generally kivy has two methods to create the root widget tree, either through overriding build() or by defining a root widget in a kv file. For more information see the documentation on creating an application.

    Your quote can be found in kivy basics, before your quoted sentence:

    Creating a kivy application is as simple as:

    I guess the authors decided to keep the basic tutorial easy and did not mention the default implementation of build, as it doesn't really do anything useful. They also omitted the kv way of defining the root widget; again I would guess to not overwhelm the reader in this first introduction.