Kivy Widgets
A widget is a fundamental component of a graphical user interface in Kivy. An interactive component that can be included in the application window or screen is a widget. Kivy comes with various pre-made widgets, including sliders, labels, text input fields, buttons, and many more. The creation of complex and interactive user interfaces is possible using these widgets.
A Kivy widget's basic form is a rectangular box that may be configured with many attributes, including color, font, size, position, and more. Each widget includes a collection of configurable settings that can be used to alter how it behaves and looks. A button widget, for example, can be set up with a label, an icon, and a callback function that is called once the button is clicked.
Widgets in the Kivy platform are arranged in a tree-like structure, with each widget having a parent and zero or more children. Widgets can be positioned and ordered in relation to one another thanks to this hierarchy. For example, numerous child widgets that are arranged in rows or columns can be contained by a container widget like a BoxLayout or GridLayout.
Label Widget:
Text is shown using the Label widget. Both static and dynamic text can be shown with it.
from kivy.app import App from kivy.uix.label import Label class MyApp(App): def build(self): return Label(text="Label Widget") if __name__ == "__main__": MyApp().run()
Button Widget:
To build buttons that can be clicked, utilize the Button widget.
from kivy.app import App from kivy.uix.button import Button class MyApp(App): def build(self): return Button(text="Button Widget") if __name__ == "__main__": MyApp().run()
TextInput Widget:
The TextInput widget is used to create a text input field.
from kivy.app import App from kivy.uix.textinput import TextInput class MyApp(App): def build(self): return TextInput() if __name__ == "__main__": MyApp().run()
Slider Widget:
The Slider widget is used to create a slider bar.
from kivy.app import App from kivy.uix.slider import Slider class MyApp(App): def build(self): return Slider() if __name__ == "__main__": MyApp().run()
CheckBox Widget:
The CheckBox widget is used to create checkboxes.
from kivy.app import App from kivy.uix.checkbox import CheckBox class MyApp(App): def build(self): return CheckBox() if __name__ == "__main__": MyApp().run()
Spinner Widget:
The Spinner widget is used to create a dropdown list.
from kivy.app import App from kivy.uix.spinner import Spinner class MyApp(App): def build(self): return Spinner(text='Select', values=('Option 1', 'Option 2', 'Option 3')) if __name__ == "__main__": MyApp().run()
Image Widget:
The Image widget is used to display images.
from kivy.app import App from kivy.uix.image import Image class MyApp(App): def build(self): return Image(source='image.png') if __name__ == "__main__": MyApp().run()
Video Widget:
The Video widget is used to display videos.
from kivy.app import App from kivy.uix.video import Video class MyApp(App): def build(self): return Video(source='video.mp4') if __name__ == "__main__": MyApp().run()
Progress Bar Widget:
The ProgressBar widget is used to create a progress bar.
from kivy.app import App from kivy.uix.progressbar import ProgressBar class MyApp(App): def build(self): return ProgressBar(max=100) if __name__ == "__main__": MyApp().run()
GridLayout Widget:
The GridLayout widget is used to create a grid layout.
from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.uix.button import Button class MyApp(App): def build(self): layout = GridLayout(cols=2) layout.add_widget(Button(text="Button 1")) layout.add_widget(Button(text="Button 2")) layout.add_widget(Button(text="Button 3")) layout.add_widget(Button(text="Button 4")) return layout if __name__ == "__main__": MyApp().run()
BoxLayout Widget:
The BoxLayout widget is used to create a box layout.
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button class MyApp(App): def build(self): layout = BoxLayout(orientation='vertical') layout.add_widget(Button(text="Button 1")) layout.add_widget(Button(text="Button 2")) layout.add_widget(Button(text="Button 3")) layout.add_widget(Button(text="Button 4")) return layout if __name__ == "__main__": MyApp().run()
There are a lot more widgets available in Kivy, and we may also design our own. There is a detailed list of widgets and their attributes in the Kivy manual.
By modifying the features of Kivy widgets, such as size, position, and color, users may create unique designs. They can also be stylized by either developing original themes or using a selection of established themes. Moreover, Kivy widgets can be animated and programmed to react to user inputs like mouse and touch events.