Friday, December 27, 2024
Google search engine
HomeLanguagesThemes and Color Palettes in KivyMD

Themes and Color Palettes in KivyMD

KivyMD is an extension of the Kivy framework. KivyMD is a collection of Material Design widgets for use with Kivy, a GUI framework for making mobile applications. It is similar to the Kivy framework but provides a more attractive GUI. In this article, we are going to see themes and color palettes in KivyMD.

Themes in KivyMD:

In order to make our application more attractive and simple to use we can use themes and different colors for our app. For changing Theme color the App Module has inbuilt function theme_cls.

theme_cls.theme_style: It has 2 options-dark and light

Syntax: self.theme_cls.theme_style=”Dark” or “Light”

Code:

Python3




# importing all necessary modules
# like MDApp, MDLabel Screen, MDTextField
# and MDRectangleFlatButton
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton
  
# creating Demo Class(base class)
class Demo(MDApp):
  
    def build(self):
        screen = Screen()
          
        # adding theme_color
        self.theme_cls.theme_style="Dark"
          
        btn = MDRectangleFlatButton(text="HI", pos_hint={
                                    'center_x': 0.5, 'center_y': 0.5},
                                    on_release=self.btnfunc)
        # adding widgets to screen
        screen.add_widget(btn)
          
        # returning the screen
        return screen
  
    # defining a btnfun() for the button to
    # call when clicked on it
    def btnfunc(self, obj):
        print("button is pressed!!")
  
  
if __name__ == "__main__":
    Demo().run()


Output:

Now, let’s see we will change the background of the theme in a light color:

Python3




# importing all necessary modules
# like MDApp, MDLabel Screen, MDTextField
# and MDRectangleFlatButton
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton
  
# creating Demo Class(base class)
class Demo(MDApp):
  
    def build(self):
        screen = Screen()
  
        # adding theme_color
        self.theme_cls.theme_style = "Light"
  
           # defining Button with all the parameters
        btn = MDRectangleFlatButton(text="HI", pos_hint={
            'center_x': 0.5, 'center_y': 0.3},
                                    on_release=self.btnfunc)
        # adding widgets to screen
        screen.add_widget(btn)
          
        # returning the screen
        return screen
  
    # defining a btnfun() for the button to
    # call when clicked on it
    def btnfunc(self, obj):
        print("button is pressed!!")
  
if __name__ == "__main__":
    Demo().run()


Output:

Changing primary palette color:

For changing colors the App Module has inbuilt function theme_cls.

  • theme_cls.primary_palette: It has a variety of colors. It accepts a string of color name.
  • theme_cls.primary_hue: It defines opaqueness of color 100 for light and A700 for dark.

Syntax: theme_cls.primary_palette=string of color name(Eg-“blue”)

              theme_cls.primary_hue=opaqueness of color

Example 1: Here we will color with the color green and opaqueness 100

Python3




# importing all necessary modules
# like MDApp, MDLabel Screen, MDTextField
# and MDRectangleFlatButton
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton
  
  
# creating Demo Class(base class)
class Demo(MDApp):
  
    def build(self):
        screen = Screen()
  
        # adding theme_color
        self.theme_cls.primary_palette = "Green"
        self.theme_cls.primary_hue = "100"
        self.theme_cls.theme_style = "Light"
  
        btn = MDRectangleFlatButton(text="HI", pos_hint={
            'center_x': 0.5, 'center_y': 0.5},
                                    on_release=self.btnfunc)
        # adding widgets to screen
        screen.add_widget(btn)
  
        # returning the screen
        return screen
  
  
    # defining a btnfun() for the button to
    # call when clicked on it
    def btnfunc(self, obj):
        print("button is pressed!!")
  
  
if __name__ == "__main__":
    Demo().run()


Output:

Example 2: with the color cyan and opaqueness A700

Python3




# importing all necessary modules
# like MDApp, MDLabel Screen, MDTextField
# and MDRectangleFlatButton
from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton
  
# creating Demo Class(base class)
class Demo(MDApp):
  
    def build(self):
        screen = Screen()
  
        # adding theme_color
        self.theme_cls.primary_palette = "Cyan"
        self.theme_cls.primary_hue = "A700"
        self.theme_cls.theme_style = "Light"
  
        btn = MDRectangleFlatButton(text="HI", pos_hint={
            'center_x': 0.5, 'center_y': 0.5},
                                    on_release=self.btnfunc)
        # adding widgets to screen
        screen.add_widget(btn)
  
        # returning the screen
        return screen
  
    # defining a btnfun() for the button to
    # call when clicked on it
    def btnfunc(self, obj):
        print("button is pressed!!")
  
if __name__ == "__main__":
    Demo().run()


Output:

RELATED ARTICLES

Most Popular

Recent Comments