Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues and updated examples in Dynamic color module #1659

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kivymd/_version.py
Original file line number Diff line number Diff line change
@@ -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"
143 changes: 72 additions & 71 deletions kivymd/dynamic_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
<ColorCard>
orientation: "vertical"
KV = '''
<ColorCard>
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
Expand Down