From 2c24739dc8a0549d6854e6953bab3519bdca211a Mon Sep 17 00:00:00 2001 From: Bageni Gilbert <135163732+bagenigilbert@users.noreply.github.com> Date: Mon, 25 Mar 2024 14:25:27 +0000 Subject: [PATCH] Fixed issues and updated examples in Dynamic color module --- kivymd/_version.py | 6 +- kivymd/dynamic_color.py | 143 ++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 74 deletions(-) diff --git a/kivymd/_version.py b/kivymd/_version.py index 5e2b80cd2..2f3531156 100644 --- a/kivymd/_version.py +++ b/kivymd/_version.py @@ -1,5 +1,5 @@ release = False __version__ = "2.0.1.dev0" -__hash__ = "f7bde69707ac708a758a02d89f14997ee468d1ee" -__short_hash__ = "f7bde69" -__date__ = "2024-02-27" +__hash__ = "4e9fdba5263b110010822c71e50c00df280405dc" +__short_hash__ = "4e9fdba" +__date__ = "2024-03-25" diff --git a/kivymd/dynamic_color.py b/kivymd/dynamic_color.py index f67070049..e7e26e8e3 100644 --- a/kivymd/dynamic_color.py +++ b/kivymd/dynamic_color.py @@ -181,97 +181,98 @@ def on_start(self): .. code-block:: python - import os + import os - from kivy.clock import Clock - from kivy.core.window import Window - from kivy.core.window.window_sdl2 import WindowSDL - from kivy.lang import Builder - from kivy.properties import StringProperty, ColorProperty +from kivy.clock import Clock +from kivy.core.window import Window +from kivy.core.window.window_sdl2 import WindowSDL +from kivy.lang import Builder +from kivy.properties import StringProperty, ColorProperty - from kivymd.uix.boxlayout import MDBoxLayout - from kivymd.app import MDApp +from kivymd.uix.boxlayout import MDBoxLayout +from kivymd.app import MDApp - KV = ''' - - orientation: "vertical" +KV = ''' + + orientation: "vertical" - MDLabel: - text: root.text - color: "grey" - adaptive_height: True + MDLabel: + text: root.text + color: "grey" + adaptive_height: True - MDCard: - theme_bg_color: "Custom" - md_bg_color: root.bg_color + MDCard: + theme_bg_color: "Custom" + md_bg_color: root.bg_color - MDScreen: - md_bg_color: app.theme_cls.backgroundColor +MDScreen: + md_bg_color: app.theme_cls.bg_dark - MDRecycleView: - id: card_list - viewclass: "ColorCard" - bar_width: 0 + MDRecycleView: + id: card_list + viewclass: "ColorCard" + bar_width: 0 - RecycleGridLayout: - cols: 3 - spacing: "16dp" - padding: "16dp" - default_size: None, dp(56) - default_size_hint: 1, None - size_hint_y: None - height: self.minimum_height - ''' + RecycleGridLayout: + cols: 3 + spacing: "16dp" + padding: "16dp" + default_size: None, dp(56) + default_size_hint: 1, None + size_hint_y: None + height: self.minimum_height +''' - class ColorCard(MDBoxLayout): - text = StringProperty() - bg_color = ColorProperty() +class ColorCard(MDBoxLayout): + text = StringProperty() + bg_color = ColorProperty() - class Example(MDApp): - def __init__(self, **kwargs): - super().__init__(**kwargs) - Window.bind(on_dropfile=self.on_drop_file) - - def on_drop_file(self, sdl: WindowSDL, path_to_file: str) -> None: - ext = os.path.splitext(path_to_file)[1] - if isinstance(path_to_file, bytes): - path_to_file = path_to_file.decode() - if isinstance(ext, bytes): - ext = ext.decode() - if ext in [".png", ".jpg"]: - self.theme_cls.path_to_wallpaper = path_to_file - Clock.schedule_once(self.generate_cards, 0.5) - - def build(self): - self.theme_cls.dynamic_color = True - self.theme_cls.theme_style = "Dark" - return Builder.load_string(KV) +class Example(MDApp): + def __init__(self, **kwargs): + super().__init__(**kwargs) + Window.bind(on_dropfile=self.on_drop_file) - def theme_switch(self) -> None: - self.theme_cls.switch_theme() + def on_drop_file(self, sdl: WindowSDL, path_to_file: str) -> None: + ext = os.path.splitext(path_to_file)[1] + if isinstance(path_to_file, bytes): + path_to_file = path_to_file.decode() + if isinstance(ext, bytes): + ext = ext.decode() + if ext in [".png", ".jpg"]: + self.theme_cls.path_to_wallpaper = path_to_file Clock.schedule_once(self.generate_cards, 0.5) - def generate_cards(self, *args): - self.root.ids.card_list.data = [] - for color in self.theme_cls.schemes_name_colors: - value = f"{color}Color" - self.root.ids.card_list.data.append( - { - "bg_color": getattr(self.theme_cls, value), - "text": value, - } - ) + def build(self): + self.theme_cls.dynamic_color = True + self.theme_cls.theme_style = "Dark" + return Builder.load_string(KV) + + def theme_switch(self) -> None: + self.theme_cls.theme_style = "Light" if self.theme_cls.theme_style == "Dark" else "Dark" + Clock.schedule_once(self.generate_cards, 0.5) + + def generate_cards(self, *args): + self.root.ids.card_list.data = [] + for color in self.theme_cls.primary_palette: + value = f"{color}" + self.root.ids.card_list.data.append( + { + "bg_color": getattr(self.theme_cls, value), + "text": value, + } + ) - def on_start(self): - super().on_start() - Clock.schedule_once(self.generate_cards) + def on_start(self): + super().on_start() + Clock.schedule_once(self.generate_cards) - Example().run() +Example().run() + .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/dynamic-color-path-to_wallpapper.gif :align: center