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.
Now in this article, we will learn about how can we create the type of button similar to floating action button using kivy python.
What is Floating Action Button ??
A floating action button (FAB) performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center.
To learn how to create this you must have the good knowledge about the Layouts, Button, canvas and Ellipse in canvas. These all we are going to use to create the button. So We are creating a button like the one we see on gmail on the right side i.e for writing a new email in mobile Application (not on website) sign.
Basic Approach: 1) import kivy 2) import kivyApp 3) import Boxlayout 4) Set minimum version(optional) 5) create Layout class 6) create App class 7) Set up .kv file : 1) Add Floating Button Properties 2) Create Main Window 3) Add Float Button 8) return Layout/widget/Class(according to requirement) 9) Run an instance of the class
Implementation of the Approach –
main.py
Python3
## Sample Python application demonstrating that ## How to create a button like floating Action Button ## in Kivy using .kv file ################################################### # import modules import kivy # base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App # BoxLayout arranges widgets in either # in a vertical fashion that # is one on top of another or in a horizontal # fashion that is one after another. from kivy.uix.boxlayout import BoxLayout # To change the kivy default settings # we use this module config from kivy.config import Config # 0 being off 1 being on as in true / false # you can use 0 or 1 && True or False Config. set ( 'graphics' , 'resizable' , True ) # creating the root widget used in .kv file class MainWindow(BoxLayout): pass # creating the App class in which name #.kv file is to be named main.kv class MainApp(App): # defining build() def build( self ): # returning the instance of root class return MainWindow() # run the app if __name__ = = '__main__' : MainApp().run() |
.kv file implementation
main.kv
Python3
#.kv file implementation of Float Button # using Float Layout for the creation of Floatbutton # Here we are creating the properties of button # Button will be created in Main window Box Layout <FloatButton@FloatLayout> id : float_root # Giving id to button size_hint: ( None , None ) text: '' btn_size: ( 70 , 70 ) size: ( 70 , 70 ) bg_color: ( 0.404 , 0.227 , 0.718 , 1.0 ) pos_hint: { 'x' : . 6 } # Adding shape and all, size, position to button Button: text: float_root.text markup: True font_size: 40 size_hint: ( None , None ) size: float_root.btn_size pos_hint: { 'x' : 5.5 , 'y' : 3.8 } background_normal: '' background_color: ( 0 , 0 , 0 , 0 ) canvas.before: Color: rgba: ( 0.404 , 0.227 , 0.718 , 1.0 ) Ellipse: size: self .size pos: self .pos # Creation of main window <MainWindow>: BoxLayout: # Creating the Float button FloatButton: text: '+' markup: True background_color: 1 , 0 , 1 , 0 |
Output: