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

Python self.theme_cls.backgroundColor set after on_start() #1674

Closed
RobertFlatt opened this issue Apr 14, 2024 · 6 comments · Fixed by #1675
Closed

Python self.theme_cls.backgroundColor set after on_start() #1674

RobertFlatt opened this issue Apr 14, 2024 · 6 comments · Fixed by #1675
Assignees
Labels
Type: Bug Bug report/Bug fix

Comments

@RobertFlatt
Copy link

RobertFlatt commented Apr 14, 2024

Description of the Bug

In the theming example https://kivymd.readthedocs.io/en/latest/themes/theming/ , the KV version works, but the Python version does not.

It fails because in the example self.theme_cls.backgroundColor == [1.0, 1.0, 1.0, 1.0] when used.
It appears the value is correctly set some time step after on_start, as shown by adding

    def on_start(self):
        super().on_start()
        print('Background  Color is ',self.theme_cls.backgroundColor)

This is, I assume, an issue with KivyMD, not with the example.

Screenshots

Python code example, expected (and KV) behavior is a dark background

Screenshot 2024-04-13 170357

Expected behavior shown in the example:

primary-palette-m3

Versions

  • OS: Windows 11
  • Python: 3.11.1
  • Kivy: 2.3.0
  • KivyMD: 2.01.dev0
@T-Dynamos T-Dynamos added the Type: Bug Bug report/Bug fix label Apr 14, 2024
@T-Dynamos T-Dynamos self-assigned this Apr 14, 2024
@T-Dynamos
Copy link
Collaborator

@RobertFlatt Hi!, Please test #1675

@RobertFlatt
Copy link
Author

@T-Dynamos

#1675 works on Windows. Thanks.

@facutoledo
Copy link

I'm experiencing a similar issue, when the application is loaded, the theme_cls.background and others are set to [1,1,1,1] value.
I'm having a similar issue with the examples.
When implementing a button to toggle theme_cls.theme_style between Dark and Light, the changes are reflected in most controls, except for the background of the screen.
To change the backgraund color of the screen, I have to do self.md_bg_color = self.theme_cls.backgroundColor within the button function.
The code below is the last thing I was traying:

from kivy.clock import Clock
from kivymd.uix.screenmanager import ScreenManager
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.label import MDLabel

class ConfigScreen(MDScreen):
    def __init__(self, **kw):
        super().__init__(**kw)
        
        self.app=MDApp.get_running_app()
        self.theme_cls = MDApp.get_running_app().theme_cls
        self.md_bg_color = self.theme_cls.backgroundColor

        list_items = MDBoxLayout(
            orientation="vertical",
        )
        list_items.add_widget(
            MDLabel(
                id="label",
                text="Press Style Button",
                font_style="Title",
                role="large"
                )
            )
        
        list_items.add_widget(
            MDButton(
                MDButtonText(
                    text="Style"),
                on_release=self.set_theme_style
            )
        )
        list_items.add_widget(
            MDButton(
                MDButtonText(
                    text="Palette"),
                on_release=self.set_color_palette
            )
        )
        self.add_widget(list_items)
    
    def set_theme_style(self, *args):
        self.theme_cls.theme_style = (
            "Dark" if self.theme_cls.theme_style == "Light" else "Light"
        )
        self.md_bg_color = self.theme_cls.backgroundColor        

    def set_color_palette(self, *args):
        self.theme_cls.primary_palette = (
            "Orange" if self.theme_cls.primary_palette == "Red" else "Red"
        )

    def volver(self, *args):
        pass

class ACC(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        self.sm = ScreenManager()
        self.sm.add_widget(ConfigScreen(name='config'))#, md_bg_color = self.theme_cls.backgroundColor))
        
    def build(self):
        self.theme_cls.theme_style_switch_animation = True
        self.theme_cls.primary_palette = "Skyblue"
        self.theme_cls.theme_style = "Dark"

        return self.sm
    
    # def on_start(self):
    #     def on_start(*args):
    #         self.root.md_bg_color = self.theme_cls.backgroundColor
    #         print(self.theme_cls.backgroundColor)

    #     Clock.schedule_once(on_start)
    
ACC().run()

When app start.
image

Press for first time Style button
image

Press for second time Style button (this was the expected result when app start)
image

Versions

  • OS: Windows 10
  • Python: 3.11.2
  • Kivy: 2.3.0
  • KivyMD: 2.01.dev0 (downloaded yesterday)

@T-Dynamos
Copy link
Collaborator

@facutoledo This is a known issue #1601 and is expected to be fixed soon.

@facutoledo
Copy link

@T-Dynamos I tried loading the screens inside the Build method, after declaring the styles. This has resulted in the background color loading when starting.
The issue still persists with the background not automatically changing when switching styles.
Below I leave the changes I made from the previous code.


class ConfigScreen(MDScreen):
     def __init__(self, **kw):
        super().__init__(**kw)
        
        self.app=MDApp.get_running_app()
        self.theme_cls = MDApp.get_running_app().theme_cls
        
    def on_pre_enter(self, *args):
        self.md_bg_color = self.theme_cls.backgroundColor

        list_items = MDBoxLayout(
            orientation="vertical",
        )    
    (...)
    def set_theme_style(self, *args):
        self.theme_cls.switch_theme()
    (...)

class ACC(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        self.sm = ScreenManager()
        
    def build(self):
        self.theme_cls.theme_style_switch_animation = True
        self.theme_cls.primary_palette = "Skyblue"
        self.theme_cls.theme_style = "Dark"

        self.sm.add_widget(ConfigScreen(name='config'))

        return self.sm
    
ACC().run()

@T-Dynamos
Copy link
Collaborator

@facutoledo Yes it is the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug report/Bug fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants