📜  KivyMD 中的主题和调色板

📅  最后修改于: 2022-05-13 01:55:22.395000             🧑  作者: Mango

KivyMD 中的主题和调色板

KivyMD 是 Kivy 框架的扩展。 KivyMD 是一组用于制作移动应用程序的 GUI 框架 Kivy 的 Material Design 小部件。它类似于 Kivy 框架,但提供了更具吸引力的 GUI。在本文中,我们将看到 KivyMD 中的主题和调色板。

KivyMD 中的主题:

为了使我们的应用程序更具吸引力和更易于使用,我们可以为我们的应用程序使用主题和不同的颜色。为了改变主题颜色,应用模块有内置函数theme_cls。

theme_cls.theme_style:它有 2 个选项——深色和浅色

代码:



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()


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()


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()


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()


输出:

现在,让我们看看我们将主题的背景更改为浅色:

蟒蛇3

# 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()

输出:

更改主调色板颜色:

为了改变颜色,应用模块有内置函数theme_cls。



  • theme_cls.primary_palette:它有多种颜色。它接受字符串颜色名称。
  • theme_cls.primary_hue:它定义了浅色 100 和深色 A700 的不透明度。

示例 1:这里我们将使用绿色和不透明度 100 进行着色

蟒蛇3

# 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()

输出:

示例 2:使用青色和不透明 A700

蟒蛇3

# 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()

输出: