Kivy - Material design Icon Button


A great open-source Python library for quick application development is called Kivy. It is quite adaptable and can function on Windows, Linux, OS X, Android, and iOS. Today, we'll concentrate on a particular Kivy feature called the Material Design Icon Button. This is a fantastic feature that will improve the visual appeal of your Kivy application.

Introduction to Kivy and Material Design

Kivy offers a comprehensive collection of UI components and is especially well-liked for creating multi-touch applications. The appeal of Kivy is its adaptability, which enables you to develop applications for a variety of devices. It's also great for projects that call for multi-touch, gestures, and other contemporary touch capabilities.

Google, on the other hand, created the design language known as Material Design. It emphasises transitions, responsive animations, and grid-based layouts among other things.

You can make stunning, practical, and platform-friendly apps by fusing Kivy's multi-platform capabilities with Material Design's aesthetics. It is made possible via the KivyMD library, a module made especially for Kivy to enable Material Design.

Understanding Material Design Icon Button

As implied by the name, the Material Design Icon Button is a button with an icon on it. It follows the guidelines of material design, providing your application a sleek and appealing appearance.

The MDIconButton class in KivyMD is used to represent Material Design Icon Buttons. An icon button's versatility, which enables users to comprehend the button function visually, is its key benefit.

Creating a Material Design Icon Button in Kivy

It's simple to make a Material Design icon button with Kivy. Installing the KivyMD module first requires running pip install kivymd from the command line.

You can make an MDIconButton in Kivy by doing the following:

from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton

class MainApp(MDApp):
   def build(self):
      btn = MDIconButton(icon="language-python")
      return btn

MainApp().run()

A straightforward Kivy application with an icon button is created in this code. The icon that the button will display is determined by the icon attribute. This name is a string that is the same as the name of an icon made with Material Design.

Examples of Material Design Icon Button in Kivy

Example 1: MDIconButton with on_release event

from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton

class MainApp(MDApp):
   def build(self):
      btn = MDIconButton(icon="language-python", on_release=self.icon_click)
      return btn

   def icon_click(self, instance):
      print("Icon button has been clicked!")

MainApp().run()

This sample makes use of the on_release event. When the button is released after being clicked, this event is triggered. "Icon button has been clicked!" is displayed on the console when the icon button has been pressed and released.

Example 2: Styling an MDIconButton

from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton

class MainApp(MDApp):
   def build(self):
      btn = MDIconButton(icon="language-python", pos_hint={'center_x': 0.5, 'center_y': 0.5}, user_font_size="64sp", theme_text_color="Custom", text_color=(1,0,0,1))
return btn

MainApp().run()

To our 'MDIconButton' in the aforementioned example, we added some customization. The button is put in the app window's middle using the 'pos_hint' attribute. The icon's size can be altered using the 'user_font_size' attribute. We can select a unique colour for the icon using the 'text_color' element because the 'theme_text_color' value is set to "Custom". Here, the icon's colour has been set to red.

Example 3: MDIconButton in .kv file

from kivymd.app import MDApp

class MainApp(MDApp):
   def build(self):
      return

MainApp().run()
# main.kv
MDIconButton:
   icon: "language-python"
   pos_hint: {'center_x': 0.5, 'center_y': 0.5}
   user_font_size: "64sp"
   theme_text_color: "Custom"
   text_color: 1, 0, 0, 1

The MDIconButton in this example is defined in a.kv file. With the help of this file, you can declaratively define your UI independently of your Python logic. For complicated applications, this strategy may result in code that is clearer and easier to manage.

Conclusion

Python programmers have a wonderful opportunity to combine the adaptability of Kivy with the aesthetics of Material Design thanks to the KivyMD module. One of the many features offered by KivyMD that enables the development of aesthetically beautiful and user-friendly applications is the Material Design Icon Button.

With the use of concrete examples, this post demonstrated how to construct and customise a Material Design Icon Button. The user experience and functionality of your Kivy applications can be improved by mastering the use of such elements. To find what works best for your application, keep trying with different icons, placements, and colours.

Updated on: 17-Jul-2023

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements