The first app
OK. Before diving straight into the book, we need at least a window to display our simulation. This means that we need to setup a very basic running app in Kivy.
To start a basic Kivy application, there are two things you need:
- An
App
class - A root
Widget
from kivy.app import App
from kivy.uix.widget import Widget
Then, you need to write a class which inherits from the App
class. This will be your app window.
class NatureApp(App):
pass
Since every running Kivy application can have one and only one root widget, we need to add the widget to the window. Here, the root widget is returned by the build
function when the App
is instantiated.
class NatureApp(App):
def build(self):
return Widget()
Finally, we just need to instantiate the App
and run the application.
if __name__ == "__main__":
NatureApp().run()
Put all elements together:
1
2
3
4
5
6
7
8
9
10
11
12
# main.py
from kivy.app import App
from kivy.uix.widget import Widget
class NatureApp(App):
def build(self):
return Widget()
if __name__ == "__main__":
NatureApp().run()
If you run the above code snippet with python main.py
in you terminal, a basic window would appear with nothing inside.
Nice.
For more details on how Kivy works under the hood, please refer to the official docs.