Search code examples
pythonkivykivymd

KivyMD MDLabels not changing colours when switching themes


I am making a GUI using kivyMD and have a switch that changes the theme between light and dark mode. I have a function that correctly switches themes when a save button is pressed, however the text on all the labels remain the same colour. How do I fix this? The kivy website shows that it automatically changes with the theme.

class MainApp(MDApp):

    def build(self):
        self.theme_cls.theme_style_switch_animation = True
        self.theme_cls.theme_style = settings["mode"]
        return Builder.load_file(r"./style.kv")

    def on_stop(self):
        cam.release()
        return super().on_stop()

    def changeTheme(self):
        self.theme_cls.theme_style = settings["mode"]

class SettingScreen(MDScreen):
    def on_pre_enter(self, *args):
        if settings["mode"] == "Dark":
            self.ids.mode.active = True
        else:
            self.ids.mode.active = False
        return super().on_pre_enter(*args)

    def save(self) -> None:
        """saves settings to file"""

        with open("settings.json", "w") as f:
            json.dump(settings, f)

        MainApp.get_running_app().changeTheme()

    def changeMode(self) -> None:
        """takes state of mode switch and updates settings dict"""
        print(1)
        if self.ids.mode.active:
            settings["mode"] = "Dark"
        else:
            settings["mode"] = "Light"

if __name__ == "__main__":
    # load settings and create camera and model objects
    settings = json.load(open(r"settings.json", "r"))
    cam = cv2.VideoCapture(0)
    cam.read()
    model = HandPoseEstim(gestModelPath=r"models\gestureModel\model\model2.tflite")

    # set window size and start app
    minSize = (480, 480)
    Window.size = minSize
    Window.minimum_width, Window.minimum_height = minSize
    MainApp().run()

<SettingScreen>
    name: "settings"
    FloatLayout:
        MDLabel:
            text: "Settings"
            halign: "center"
            font_size: 50
            pos_hint: {"center_x": .5, "center_y": .85}

        MDRaisedButton:
            text: "Return"
            on_press:
                root.manager.current = "menu"
                root.manager.transition.direction = "down"
            size_hint: (.05,.1)
            pos_hint: {"center_x": .1, "center_y": .9}

        MDSwitch:
            id: mode
            active: False
            on_active: root.changeMode()
            pos_hint: {"center_x": .5, "center_y": .5}
        
        MDRaisedButton:
            id: save
            text: "Save"
            on_press: root.save()

i have tried using both labels and mdlabels, also tried manually changing them


Solution

  • I have the same problem with kivymd==1.1.0. When I installed kivymd==1.0.2 everything works fine.