Create spinner using KivyMD
Last Updated :
09 Oct, 2022
In this article, we will learn how to create a spinner using KivyMD in Python programming language.
Create spinner using KivyMD
In addition to the Kivy framework is KivyMD. A set of Material Design widgets for use with the Kivy GUI framework which is used to create mobile applications. The Kivy framework is comparable, however, it offers a more appealing GUI. To install the kivymd module execute the below command in the command prompt or terminal.
pip install kivymd
Example 1
In this example, we are going to create a simple spinner that we can see normally while buffering. Here is the stepwise implementation.
Step 1. Import required packages.
Python3
from kivy.lang import Builder
from kivymd.app import MDApp
|
Step 2. Adjusting window size. To adjust window size we pass width and height to window.size as done below.
Step 3. We will be designing our layout using Kv language. First, we will declare the layout widget class called MDScreen and then the child widget class called MDSpinner. For MDSpinner we will pass its location in x,y coordinate form. center_x is used for the x coordinate whereas center_y is used for the y coordinate. To set Spinner size we will be using the size and set its dp to 46. We will keep active to True in order to get spinner active by default.
Step 4. Writing the main program. To run Kv file we will be using load_string() and pass our Kv language in it. So we will define a function for this named build() and on-call it will load Kv and return the screen. run() is used to run the class and it does not require any parameters.
Python3
class Main(MDApp):
def build( self ):
return Builder.load_string(KV)
Main().run()
|
Complete code:
Python3
from kivy.lang import Builder
from kivymd.app import MDApp
KV =
class Main(MDApp):
def build( self ):
return Builder.load_string(KV)
Main().run()
|
Output:
Spinner
Example 2
In this example, we will be making changes in Step 2 of the above example and keep other steps unchanged.
We will add MDCheckBox and specify its position using pos_hint and give it an id name check. If the check box is checked then it will have active as True so we will be using the same approach in MDSpinner and check if MDCheckBox is returning active as True or False, if returns true we will set MDspinner True. To do so we use active in MDSpinner and check for active status in MDCheckBox using check.active.
Complete Code:
Python3
from kivy.lang import Builder
from kivymd.app import MDApp
KV =
class Main(MDApp):
def build( self ):
return Builder.load_string(KV)
Main().run()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...