Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Use the KV Language

You can learn more in the KV language guide.

00:00 Using the KV language. Kivy also provides a design language called KV that you can use with your Kivy applications. The KV language lets you separate your interface design from the application’s logic.

00:15 This follows the separation of concerns principle and is part of the Model-View-Controller architectural pattern. On-screen, you’ll see a simple example, which will make use of the KV language.

00:44 This code might look a bit odd at first glance, as it creates a Button without setting any of its attributes or binding it to any events.

00:54 What’s happening here is that Kivy will automatically look for a file that has the same name as the class in lowercase, without the App part of the class name.

01:03 In this case, the class name is ButtonApp, so Kivy will look for a file named button.kv. If that file exists and is properly formatted, then Kivy will use it to load up the user interface.

01:17 Go ahead and create the file and add the code seen on-screen. This matches the Button call in your Python code. It tells Kivy to look into the instantiated object for a button definition.

01:33 This sets the button text, and here you set the width and height with size_hint.

01:43 size_hint tells Kivy the proportions to use when creating the widget. It takes two numbers. The first number is the x size hint and refers to the width of the control.

01:52 The second number is the y size hint and refers to the height of the control. Both of these numbers can be anywhere between 0 and 1.

02:01 The default value for both hints is 1. Next, you set the buttons position with pos_hint. Here, you tell Kivy to center the widget on the x and y axes with a value of .5 for each.

02:17 This line sets the on_press event handler. To tell Kivy where the event handler is, you use app.on_press_button(). Here, Kivy will look in the Application class for a method called .on_press_button().

02:33 With both files saved, running the kv_example script works as expected, with Kivy pulling the information from button.kv behind the scenes and creating the layout as expected.

02:44 With .on_press_button() working as desired,

02:51 you can set up all your widgets and layouts inside one or more KV language files. The KV language also supports importing Python modules in KV, creating dynamic classes, and much more.

03:05 For full details, check out Kivy’s guide to the KV language. Now you’re ready to create a real application, and that’s what you’ll be doing in the next section of the course.

Become a Member to join the conversation.