Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
StackLayout:
To use StackLayout firt import StackLayout by below command:
from kivy.uix.stacklayout import StackLayout
The StackLayout arranges children vertically or horizontally, as many as the layout can fit. The size of the individual children widgets does not have to be uniform. There are 4 row-wise and 4 column wise orientations.
StackLayout Orientation (2D): - right to left or left to right - top to bottom or bottom to top - 'rl-bt', 'rl-tb', lr-bt', 'lr-tb'(Row wise) - 'bt-rl', 'bt-lr', 'tb-rl', 'tb-lr'(Column wise)
Basic Approach to create Stack layout : 1) import kivy 2) import kivyApp 3) import Button 4) import Stacklayout 5) Set minimum version(optional) 6) Create the StackLayout class 7) Create the App class 8) Set up .kv file (name same as App class) 9) return StackLayout Class 10) Run an instance of the class
Below is the implementation of row-wise orientations and column-wise orientation:
main.py file –
# code to show how to use StackLayout using .kv file # base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App # creates the button in kivy # if not imported shows the error from kivy.uix.button import Button # The StackLayout arranges children vertically # or horizontally, as many as the layout can fit. from kivy.uix.stacklayout import StackLayout # creating the root widget used in .kv file class StackLayout(StackLayout): pass # class in which name .kv file must be named Slider.kv. # or creating the App class class StackApp(App): def build(self): # returning the instance of StackLayout class return StackLayout() # run the app if __name__=='__main__': StackApp().run() |
The name of the .kv file must be same as the App class i.e Stack.kv
.kv file –
<StackLayout>: # Different orentation # ['lr-tb', 'tb-lr', 'rl-tb', 'tb-rl', 'lr-bt', 'bt-lr', 'rl-bt', 'bt-rl'] orientation: 'lr-tb' # Creating Multiple Buttons Button: text: 'B1' size_hint: [.2, .1] Button: text: 'B2' size_hint: [.2, .1] Button: text: 'B3' size_hint: [.2, .1] Button: text: 'B4' size_hint: [.2, .1] Button: text: 'B5' size_hint: [.2, .1] Button: text: 'B6' size_hint: [.2, .1] Button: text: 'B7' size_hint: [.2, .1] Button: text: 'B8' size_hint: [.2, .1] Button: text: 'B9' size_hint: [.2, .1] Button: text: 'B10' size_hint: [.2, .1] |
Output:

This is for the orientation ‘lr-tb’. First the widgets are added left-to-right and then top-to-bottom.
Note: If want to change orientation just change the orientation in line no 04 of the .kv file with any of the below orientations –
For row wise orientation use: -'lr-tb' -'lr-bt' -'rl-tb' -'rl-bt' For column wise orientation use: -'tb-lr' -'tb-rl' -'bt-lr' -'bt-rl'
Below there are picture output all the orientations above –
For row wise orientation use:
'lr-tb'
Output:

'lr-bt'
Output:

'rl-tb'
Output:

'rl-bt'
Output:

For column wise orientation use:
'tb-lr'
Output:

'tb-rl'
Output:

'bt-lr'
Output:

'bt-rl'
Output:

Recommended Posts:
- Python | StackLayout in Kivy
- Python | Create a stopwatch using clock object in kivy using .kv file
- Circular (Oval like) button using canvas in kivy (using .kv file)
- Python | AnchorLayout in Kivy using .kv file
- Python | FloatLayout in Kivy using .kv file
- Python | Relative Layout in Kivy using .kv file
- Python | PageLayout in Kivy using .kv file
- Python | Adding image in Kivy using .kv file
- Python | Switch widget in Kivy using .kv file
- Python | Spinner widget in Kivy using .kv file
- Python | Popup widget in Kivy using .kv file
- Python | ScreenManager in Kivy using .kv file
- Python | Canvas in Kivy using .kv file
- Python | Carousel Widget In Kivy using .kv file
- Python | Animation in Kivy using .kv file
- Python | Progressbar widget in kivy using .kv file
- Python | Accordion in kivy using .kv file
- Python | TextInput in kivy using .kv file
- Python | Drop-down list in kivy using .kv file
- Python | Toggle button in kivy using .kv file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

