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.
In this article, we will see that how can we can change the size and the position of button in kivy Python. There are 4 properties to set up, the size and position of button. There are 2 properties which are for static placement and another 2 are for dynamic placement.
size : It takes two arguments i.e. (width, height).
Python3
b1 = Button(size =(100, 100))
b2 = Button(size =(200, 200))
pos : pos stands for position i.e it is used to position the widget. By default (0, 0), the bottom-left corner of the screen is the default position of the button in kivy python.
Python3
b1 = Button(pos =(100, 100))
b2 = Button(pos =(200, 200))
size_hint : Provide hint of size. It contains two arguments i.e. width and height it can be floating values. By default, all widgets have their size_hint=(1, 1). To create a button 50% of the width and 25% of the height of the layout and positioned at (20, 20), you can do -
Python3
button = Button(
text ='Hello world',
size_hint =(.5, .25),
pos =(20, 20))
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)}
If you want to create a button that will always be the size of layout minus 20% on each side -
Python3
button = Button(text ='Hello world', size_hint =(.6, .6),
pos_hint ={'x':.2, 'y':.2})