Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to use the FloatLayout widget to create buttons of at different positions in a window. The position of the button can be absolute or relative with respect to the bigger window.
With Absolute position
In this approach we set the position of the button at a fixed position. So when the resize of the window happens, the size of the window and the size of the button changes but the position of the button remains constant. So the button becomes hidden after sometimes depending on how the window is squeezed.
Example
import kivy from kivy.app import App from kivy.uix.button import Button from kivy.uix.floatlayout import FloatLayout #from kivy.config import Config #Config.set('graphics', 'resizable', True) # creating the App class class FloatApp(App): def build(self): # creating Floatlayout Flt = FloatLayout() btn = Button(text='Hello world', size_hint=(0.5 ,0.2), pos=(100, 200), background_normal='', background_color=(1, 0, 1, 1)) # adding button widget Flt.add_widget(btn) return Flt # Run the app FloatApp().run()
Running the above code gives us the following result −
With relative position
In this approach we set the position of the button with position hint. So when the resize of the window happens, the size of the window and the size of the button changes and also the position of the button keeps changing with respect to the relative size of the window. So the button never becomes hidden when the window is squeezed.
Example
from kivy.app import App from kivy.uix.button import Button from kivy.uix.floatlayout import FloatLayout from kivy.config import Config Config.set('graphics', 'resizable', True) # creating the App class class FloatApp(App): def build(self): # creating Floatlayout Flt = FloatLayout() btn = Button(text='Hello world', size_hint=(0.2 ,0.2), pos_hint={'x':.2, 'y':.2 }, background_normal='', background_color=(1, 1, 0, 0.8) ) # adding button widget Flt.add_widget(btn) return Flt # Run the app FloatApp().run()
Output
Running the above code gives us the following result −