Search code examples
pythonkivykivy-languagekivymd

KivyMD when switching between screens the palette and the elevation gets lost


I found that whenever I have multiple screens (managed by the MDScreenManager) and I switch between them, certain properties (the elevation of the cards on the screens, or the changes on the primarly palette) are lost.

  1. Run the project: enter image description here

  2. Switch to dark mode (theme and palette changes working fine): enter image description here

  3. Change to the second screen (in dark or light mode, doesn't matter): enter image description here

  4. Come back to the first creen (I already lost the MDCard elevation): enter image description here

  5. When switching between themes, the primarly palette has been lost on the dark mode: enter image description here

This is my main.py:

from kivy.config import Config
Config.set('graphics', 'fullscreen', 0)
Config.set('graphics', 'window_state', 'maximized')

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.screen import MDScreen
from kivy.core.window import Window

class Home(MDScreen):
    pass

class Settings_1(MDScreen):
    pass

Window.size = (800, 480)

sm = MDScreenManager()
sm.add_widget(Home(name='home'))
sm.add_widget(Settings_1(name='settings_1'))


class Test(MDApp):

    dialog = None
    def build(self):
        self.theme_cls.primary_palette = "Blue"
        self.theme_cls.theme_style = "Light"
        self.theme_cls.theme_style_switch_animation = True
        self.theme_cls.material_style = "M3"

        return Builder.load_file('kivy.kv')

    def on_checkbox_active(self, value):
        if not value.active:
            self.theme_cls.primary_palette = "Gray"
            self.theme_cls.theme_style = "Dark"
        else:
            self.theme_cls.primary_palette = "Blue"
            self.theme_cls.theme_style = "Light"
        return


if __name__ == "__main__":
    print('Starting')

    Test().run()

and this the kivy.kv file:

ScreenManager:
    Home:
    Settings_1:

<Home>:
    name:'home'
    MDCard:
        elevation: 3
        orientation: 'vertical'
        adaptive_height: True
        radius: 20
        pos_hint: {"center_x": .5, "center_y": .5}
        size_hint: 0.3, 0.3
        md_bg_color: app.theme_cls.bg_darkest
        padding: dp(5), dp(10)
        spacing: dp(10)
        MDLabel:
            text: 'Label 1'
            halign: 'center'
            pos_hint: {'center_x': 0.5,'center_y':0.8}

        MDSeparator:

        MDSwitch:
            active: True
            icon_active: "white-balance-sunny"
            icon_inactive: "moon-waning-crescent"
            on_active: app.on_checkbox_active(self)
            theme_text_color: app.theme_cls.theme_style
            pos_hint: {'center_x': 0.5,'center_y':0.5}

        MDSeparator:

        MDRectangleFlatButton:
            text: 'Next screen'
            pos_hint: {'center_x': 0.5,'center_y':0.2}
            on_press:
                root.manager.current ='settings_1'
                root.manager.transition.direction = 'left'

<Settings_1>:
    name:'settings_1'

    MDCard:
        elevation: 3
        orientation: 'vertical'
        adaptive_height: True
        radius: 20
        pos_hint: {"center_x": .5, "center_y": .5}
        size_hint: 0.3, 0.3
        md_bg_color: app.theme_cls.bg_darkest
        padding: dp(5), dp(10)
        spacing: dp(10)
        MDLabel :
            text: 'Label 2'
            halign: 'center'
        MDRectangleFlatButton:
            text: 'Previous screen'
            pos_hint: {'center_x': 0.5,'center_y':0.2}
            on_press:
                root.manager.current ='home'
                root.manager.transition.direction = 'right'

Solution

  • I had that problem too, turns out that its a bug in kivyMD version 1.1.1, it is solved when I upgraded to version 1.2.0, as recommended in: https://github.com/kivymd/KivyMD/issues/1439#issuecomment-1365039955

    BTW, it will demend upgarding to a non-released version of kivy as well (2.2.0.dev0) which was the main difficulty here - as wheels are not available for this version, I had to install some additional GL files for it to work.