Themes and Color Palettes in KivyMD

06 Jun, 2021
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.
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 MDRectangleFlatButtonfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom 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 MDRectangleFlatButtonfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom 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:
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 MDRectangleFlatButtonfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom 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 MDRectangleFlatButtonfrom kivymd.app import MDAppfrom kivymd.uix.screen import Screenfrom 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:
Python-gui
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Create a directory in Python