Kivy .kv Language cont'd

In this tutorial, we cover how to organize your presentation structure. So far, we've just shown how to add a label, but there is much more to the Kivy language than this. For example, you can either dictate what all buttons will look like, for what specific buttons in specific widgets look like, or both!

To show this, we're going to add another class, Widgets, and then we'll see how to describe what a button within a specific class should look like:

We're now importing Widget, and then we're making this Widgets class, which is inheriting from the Widget class from Kivy.

Now, where we used to return a different Kivy class, we're going to instead have SimpleKivy (now called SimpleKivy2, which will correspond to my SimpleKivy2.kv file) now returning the Widgets class.

That's all for the Python code. We pass with the Widgets class because the only "code" that we want for now is just presentation code. Since we're currently using .kv files for the presentation, that's where the rest of our code will be going.

Now, what is happening here is that is referencing our Widgets class from the main python file. That has Widgets acting as a parent here, and now, using tabs, we can define some children of the Widget.

First, we see two button children. After that, we see another tab with a some parameters for those buttons.

It will be very easy to forget the required colons after the parents, and you won't really be served any explicit errors because of it.

The outcome for this:

For the buttons: Size is the width and height of the button in pixels. Then, pos is the position of the element, using the top left point as the guide. Next, "Text" is whatever we want the button to say. Then we have color, which is measured as RGB-A, or red, green, blue, alpha, and these are in values between 0 and 1. So, 0,1,0,1 is no red, full green, no blue, and full alpha (meaning the color is solid). Finally, "font_size" is the size of the font. I know you didn't see that coming!