For more information on laying out the user interface, check out the Kivy layout documentation.
Lay Out the User Interface (UI)
00:00 Laying Out the UI. Each GUI framework that you use has its own method of arranging widgets. For example, in wxPython, you would use sizers, whereas in Tkinter, you use a layout or geometry manager. With Kivy, you use layouts.
First, you import
kivy.uix.boxlayout. Next, you create some colors, which are lists of RGB and alpha values. These are then added to another list,
colors, which becomes a list of lists, allowing easy random selection of these preselected colors.
When you run this code, you’ll see something similar to what’s seen on-screen, although your colors will be a random choice. When you create a layout, there are few arguments you should know.
padding: You can specify the padding in pixels between the layout and its children in one of three ways: A four-argument list
[padding_left, padding_top, padding_right,
padding_bottom], a two-argument list
padding_vertical], or a single argument of
02:51 Like most GUI tool kits, Kivy is mostly event-based. The framework responds to user keypresses, mouse events, and touch events. Kivy has a concept of a clock that you can use to schedule function calls for some time in the future.
Kivy also has the concept of
Properties, which works with the
EventDispatcher. Properties help you to do validation checking. They also let you fire events whenever a widget changes its size or position.
Let’s add a button event to the previous example. First, an
.index attribute is added to the
Button object. This will be used by the function that you’ll add later. Next, you call
button.bind(), and this links the
on_press event to the
MainApp.on_press_button() method, which you’re going to code in a second.
Become a Member to join the conversation.