Install External Libraries use the pip
package manager to install the desired libraries from the Python Package Index (PyPI). For example, to install numpy
:
Import and Use Within your Kivy application code (typically a Python file), import the external libraries you need and use their functions as you would in a regular Python script:
import kivy
from kivy.app import App
class KivyApp(App):
def build(self):
pass
KivyApp().run()
1- Label
Position Label:
- Use the
pos
property to set the Label's position in pixels relative to the window's top-left corner.
Aligning Text:
- Align the text within the Label using the
halign
andvalign
properties. These properties take values like 'left', 'center', and 'right' for horizontal alignment and 'top', 'center', and 'bottom' for vertical alignment. However, by default, these properties only work iftext_size
is set.
2 - Button
In Kivy, a Button is widget that allows users to interact with your application by clicking or pressing it. Here's a closer look at how to work with Buttons in Kivy:
Create a Button:
- Buttons are created using the
Button
class from thekivy.uix.button
module.
Customize Button Text:
- Set the text displayed on the button using the
text
property.
Functionality:
- Buttons become interactive by binding functions to events. The primary events for buttons are
on_press
andon_release
. on_press
: This event triggers a function when the button is pressed or clickedon_release
: This event triggers a function when the button is released after a click or press.
3 - BoxLayout
BoxLayout is a fundamental layout manager used to organize widgets in a horizontal or vertical direction. It's a simple yet powerful tool for structuring user interfaces.
- Function: Arrange child widgets sequentially, either one on top of another (vertically) or side-by-side (horizontally).
- Orientation: Controlled by the
orientation
property. By default (or if left blank), it's horizontal. Set it to"vertical"
for a vertical layout.
Common Use Cases:
- Building rows or columns of widgets (buttons, labels, etc.)
- Creating basic app screens with a clear organization.
- Nesting Box layouts within other layouts for more complex structures.
Key Properties:
orientation
: Defines the layout direction (horizontal or vertical).spacing
: Sets the space (in pixels) between child widgets.padding
: Sets the padding (in pixels) around the outer edges of the BoxLayout.
4 - GridLayout
GridLayout is another important layout manager used for arranging widgets in a grid-like structure. It offers more flexibility compared to BoxLayout for creating matrices of elements.
Function:
- Divide the available space into rows and columns.
- Arrange child widgets within these cells, following the order they are added.
Key:
- Requires defining either
cols
orrows
property, or both, to specify the number of columns and rows in the grid. - Unlike BoxLayout, you cannot explicitly position widgets in specific cells. Their placement depends on the layout configuration and their order in the
children
list. - Provides properties for fine-tuning the appearance of the grid:
col_default_width
: Sets the default width for columns (if not specified by widgets'size_hint
).row_default_height
: Sets the default height for rows (if not specified by widgets'size_hint
).spacing
: Sets the space (in pixels) between cells.padding
: Sets the padding (in pixels) around the outer edges of the GridLayout.
5 - TextInput
Properties:
text
: Stores the current text content displayed within the TextInput.hint_text
: Sets a placeholder text displayed when the TextInput is empty, guiding users on what kind of input is expected.multiline
: Controls whether the TextInput allows entering multiple lines of text (True) or restricts it to a single line (False, default).readonly
: Makes the TextInput non-editable, allowing users to only view the existing text.password
: Hides the entered text with asterisk characters (*), useful for passwords
6 - Change Window Color
There are two main ways to change the window background color in Kivy:
Using Window.clearcolor:
This is the simpler approach and directly sets the clear color for the entire window.
Using canvas instructions:
This method offers more flexibility if you want to achieve specific effects or have a background element behind your widgets.
Use Window.clearcolor
for a simple solid color background for the entire window.
7- Change Window Width
There are two primary methods to change the window width in Kivy:
Using Config
before window creation:
This approach is ideal for setting the initial window size before your application launches. It involves modifying the configuration settings.
Using Window.size
after window creation:
This method allows you to dynamically change the window width during runtime.
8 - Image
Image
widget from the kivy.uix
module. Here's a breakdown of how to incorporate images into your Kivy application.Specifying the Image Source:
The Image
widget has a source
property that determines the location of the image file.
Path: If the image is located elsewhere on your system, use an absolute path.
Adding the Image to Your Layout:
Once you have created the Image
widget with the source specified, you need to add it to your application's layout using a layout manager like BoxLayout
or GridLayout
.
9 - Anchor Layout
Function:
- Anchors child widgets to specific borders (top, bottom, left, right) or the center of the AnchorLayout.
- Offers precise control over the positioning of each child widget.
Properties:
anchor_x
: This OptionProperty defines the horizontal anchor point for the widget. It accepts values like:"left"
: Anchors the widget to the left border of the AnchorLayout."center"
(default): Positions the widget horizontally centered within the AnchorLayout."right"
: Anchors the widget to the right border of the AnchorLayout.
anchor_y
: Similar toanchor_x
, this OptionProperty controls the vertical anchor point. It accepts values like:
"top"
: Anchors the widget to the top border of the AnchorLayout."center"
(default): Positions the widget vertically centered within the AnchorLayout."bottom"
: Anchors the widget to the bottom border of the AnchorLayout.
10 - PageLayout
Function:
- Manages a stack of child widgets, treating each one as a separate page.
- Enables users to swipe left or right across the screen to navigate between pages.
- Provides visual cues (like borders) to indicate switchable pages.
Properties:
page
: NumericProperty representing the index of the currently displayed page (0-based indexing).border
: NumericProperty defining the width of the border around the current page, used for swipe detection.swipe_threshold
: NumericProperty (default 0.5) specifying the proportion of the screen width required for a swipe gesture to register as a page change.
11 - FloatLyout
Concepts:
- pos_hint: A dictionary property that defines the relative position of a widget within the FloatLayout. It takes values between 0.0 (left/bottom edge) and 1.0 (right/top edge) for the
x
(horizontal) andy
(vertical) coordinates.- Example:
pos_hint={'x': 0.2, 'y': 0.3}
positions the widget 20% from the left edge and 80% from the bottom edge.
- Example:
- size_hint: Another dictionary property that specifies the relative size of a widget compared to the FloatLayout itself. It also uses values between 0.0 and 1.0 for width and height.
- Example:
size_hint={'width': 0.5, 'height': 0.5}
sets the widget to half the width and one-third the height of the FloatLayout.
- Example:
pos_hint
and sized with size_hint
. Button 1 is placed in the top left corner, and Button 2 is centered horizontally and positioned halfway down the layout.