The KV language is a powerful and expressive way to define the user interface of Kivy applications. Below is a detailed explanation of its basic syntax:
Widgets and Properties Define widgets and set their properties directly.
Button:
text: 'Click Me'
size_hint: 0.5, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Nesting Widgets :Nest widgets inside each other to create complex layouts.
BoxLayout:
orientation: 'vertical'
Label:
text: 'Hello, World!'
Button:
text: 'Press Me'
ID References :Assign IDs to widgets for easy reference.
BoxLayout:
orientation: 'vertical'
Label:
id: my_label
text: 'Hello, World!'
Button:
text: 'Press Me'
on_press: my_label.text = 'Button Pressed!'
Class RulesDefine properties that apply to all instances of a specific class.
<Button>:
font_size: 24
color: 1, 0, 0, 1 # Red text
Custom WidgetsCreate custom widgets by extending existing ones using the @ syntax.
<CustomButton@Button>:
background_color: 0, 0, 1, 1 # Blue background
Dynamic Classes Define reusable widget templates.
<CustomLabel@Label>:
font_size: 24
color: 0, 1, 0, 1 # Green text
BoxLayout:
CustomLabel:
text: 'Custom Label 1'
CustomLabel:
text: 'Custom Label 2'
Property Bindings : Bind properties of widgets to data, enabling dynamic updates.
Label:
text: app.label_text
Event BindingsBind events to methods using the on_event syntax.
Button:
text: 'Click Me'
on_press: app.on_button_press()