In this article, we will develop a GUI window using kivy framework of Python.
Kivy is a platform-independent GUI tool in Python. It is an open-source Python library for the rapid development of multi-touch applications on Windows, macOS, Android, iOS, Linux, and Raspberry Pi. KivyMD package is a collection of material Design compliant widgets which can be used with the Kivy module. In this article, we will develop a GUI window using kivy framework of Python, and we will add available font Styles in KivyMD
Installation:
To install the modules type the below command in the terminal.
pip install kivy
pip install kivymd
Available font Styles in KivyMD:
S. No | Font Style |
---|---|
1 | H1 |
2 | H2 |
3 | H3 |
4 | H4 |
5 | H5 |
6 | H6 |
7 | Subtitle1 |
8 | Subtitle2 |
9 | Body1 |
10 | Body2 |
11 | Button |
12 | Caption |
13 | Overline |
14 | Icon |
Method 2: Doing with Kv language.
Step 1: Import required packages.
For this, we will need to import Builder from kivy and MDApp, MDLabel from kivymd package.
Python3
installing packages from kivy.lang import Builder from kivymd.app import MDApp from kivymd.uix.label import MDLabel |
Step 2: Design layout using kv language.
First, we will declare the Screen widget class called MDScreen and then the child widget class. Here, MDBoxLayout is a child class, and MDToolbar is Sub-child class.
We will be using id and orientation in our MDBoxLayout, here our id will be box and we will set our orientation to vertical. MDToolbar will have a single parameter called title. The title works as an output text on MDToolbar.
Python3
# kv lang KV = ''' # declaring screen MDScreen: #this will create vertical box layout MDBoxLayout: id: box orientation: "vertical" MDToolbar: title: "MDFonts" ''' |
Step 3: Writing the main program.
To run kv file we will be using load_string() and pass our kv language in it and we will define a function named build() and when called, it will load kv and retn the screen. run() is used to run the class and does not require any parameters.
To display multiple fonts we will use a for loop and add the widget one by one using add_widget() and we will pass the MDLabel in it. Here MDLabel takes 3 parameters:
Syntax: MDLabel(text=””, halign=””,font_style=””)
where text is our output text, halign is used for alignment of the text so for this example we used to center and font_style is the font we want to use for text. You can choose any font listed in the above table.
Implementation:
Python3
# installing packages from kivy.lang import Builder from kivymd.app import MDApp from kivymd.uix.label import MDLabel # kv lang KV = ''' # declaring screen MDScreen: #this will create vertical box layout MDBoxLayout: id: box orientation: "vertical" MDToolbar: title: "MDFonts" ''' # app class class Test(MDApp): def build( self ): # this will load kv lang screen = Builder.load_string(KV) # running loop for all the declared font name for font_name in [ "H1" , "H2" , "Subtitle1" , "Button" , "Overline" , "Caption" , "Icon" , ]: # this will add widgets to the screen # for all font_name # declared in list screen.ids.box.add_widget( MDLabel( text = "GEEKSFORGEEKS" , halign = "center" , font_style = font_name, ) ) # returning screen return screen # running app Test().run() |
Output:
Method 2: Doing the same without Kv language.
Step 1: Importing packages.
Python3
# importing packages from kivymd.app import MDApp from kivymd.uix.label import MDLabel from kivymd.uix.boxlayout import MDBoxLayout |
Step 2: Writing the main program.
In this step, the main function will be similar to the above example. The only difference here is that we are defining MDBoxLayout in the main function and passing the orientation parameter. Now we will apply the same loop as done in the above example. After all this, we will run the MainApp class.
Python3
# defining MainApp class class MainApp(MDApp): def build( self ): # this will set MDBoxLayout to vertical screen = MDBoxLayout(orientation = 'vertical' ) # running loop for all the declared font name for name_theme in [ "H1" , "H2" , "Subtitle1" , "Button" , "Overline" , "Caption" , "Icon" , ]: # this will add widgets to the screen # for all font_name # declared in list screen.add_widget( MDLabel( text = "GEEKSFORGEEKS" , halign = "center" , font_style = name_theme, ) ) # returning screen return screen # running app MainApp().run() |
Implementation:
Python3
# importing packages from kivymd.app import MDApp from kivymd.uix.label import MDLabel from kivymd.uix.boxlayout import MDBoxLayout # defining MainApp class class MainApp(MDApp): def build( self ): # this will set MDBoxLayout to vertical screen = MDBoxLayout(orientation = 'vertical' ) # running loop for all the declared font name for name_theme in [ "H1" , "H2" , "Subtitle1" , "Button" , "Overline" , "Caption" , "Icon" , ]: # this will add widgets to the screen # for all font_name # declared in list screen.add_widget( MDLabel( text = "GEEKSFORGEEKS" , halign = "center" , font_style = name_theme, ) ) # returning screen return screen # running app MainApp().run() |
Output: