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.
???????? Kivy Tutorial – Learn Kivy with Examples.
Relative Layout:
- This layout operates in the same way as FloatLayout does, but the positioning properties (x, y, center_x, right, y, center_y, and top) are relative to the Layout size and not the window size.
- In reality regardless of absolute and relative positioning, the widgets are moved when the position of the layout changes.
- The available pos_hint keys (x, center_x, right, y, center_y, and top) are useful for aligning to edges or centering. For example: pos_hint: {‘center_x’:.5, ‘center_y’:.5} would align a Widget in the middle, no matter what the size of the window is.
The first thing we need to do to use a RelativeLayout is import it.
from kivy.uix.relativelayout import RelativeLayout
We can do relative positioning by: pos_hint: provide hint of position. We can define upto 8 keys i.e. it takes arguments in form of dictionary. pos_hint = {“x”:1, “y”:1, “left”:1, “right”:1, “center_x”:1, “center_y”:1, “top”:1, “bottom”:1(“top”:0)}
Note: Floatlayout and RelativeLayout both support absolute and relative positioning depending upon whether pos_hint or pos is used.But If you want absolute positioning, use the FloatLayout.
Basic Approach to create Relative Layout:
1) import kivy
2) import kivy App
3) import Relativelayout
4) Set minimum version(optional)
5) create class for layout
6) create App class:
- define build() function
7) Set up.kv file(name same as the App class)
8) return Layout Class
9) Run an instance of the class
Below is the Implementation: main.py file
Python3
import kivy
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.config import Config
Config. set ( 'graphics' , 'resizable' , True )
class RelativeLayout(RelativeLayout):
pass
class Relative_LayoutApp(App):
def build( self ):
return RelativeLayout()
if __name__ = = "__main__":
Relative_LayoutApp().run()
|
.kv file implementation :
Python3
<Button>:
font_size: 20
size_hint: 0.2 , 0.2
<RelativeLayout>:
canvas:
Color:
rgb: 0 , 1 , 1
Rectangle:
pos:[ 0.2 * coord for coord in self .size]
size:[ 0.6 * coord for coord in self .size]
Button:
text:"B1"
background_color: 0.1 , 0.5 , 0.6 , 1
pos_hint: {"x": 0 , "y": 0 }
Button:
text:"B2"
background_color: 1 , 0 , 0 , 1
pos_hint: {"right": 1 , "y": 0 }
Button:
text:"Yash"
background_color: 0.4 , 0.5 , 0.6 , 1
pos_hint: {"center_x":. 5 , "center_y":. 5 }
Button:
text:"B3"
background_color: 0 , 0 , 1 , 1
pos_hint: {"x": 0 , "top": 1 }
Button:
text:"B4"
background_color: 0.8 , 0.9 , 0.2 , 1
pos_hint: {"right": 1 , "top": 1 }
|
Output: Different size of window images are there which show that How it adjust itself accordingly to window i.e relatively Image 1:
Image 2:
Image 3:
Reference: https://kivy.org/doc/stable/api-kivy.uix.relativelayout.html