Search code examples
pythonpython-3.xkivykivy-language

How to update a kivy label color thats data is store in a variable in the main app class during runtime


here is my py file

class ImportSetting:
    with open('settings.txt') as json_file:
        save_data = json.load(json_file)

class MainApp(App):
    height = Window.height
    spacing = dp(2)
    def sp(self, a):
        self.spacing = a
    # Button Colors #################################
    button_color_square = ImportSetting.save_data['bcs']
    def bcs(self, val):
        self.button_color_square = val

    button_text_color_square = ImportSetting.save_data['bcs_t']
    def bcs_t(self, val):
        self.button_text_color_square = val

    button_color_top_bar = ImportSetting.save_data['bct']
    def bct(self, val):
        self.button_color_top_bar = val

    button_text_color_top_bar = ImportSetting.save_data['bct_t']
    def bct_t(self, val):
        self.button_text_color_top_bar = val

    button_color_side_bar = ImportSetting.save_data['bcr']
    def bcr(self, val):
        self.button_color_side_bar = val

    button_text_color_side_bar = ImportSetting.save_data['bcr_t']
    def bcr_t(self, val):
        self.button_text_color_side_bar = val

    # Background Color
    text_background_color = ImportSetting.save_data["text_bc"]
    def text_bc(self, val):
        self.text_background_color = val

    button_background_color = ImportSetting.save_data["button_bc"]
    def button_bc(self, val):
        self.button_background_color = val

    history_background_color = ImportSetting.save_data["history_bc"]
    def history_bc(self, val):
        self.history_background_color = val

    # Text color
    text_color_main = ImportSetting.save_data["main_tc"]
    def tc(self, val):
        self.text_color_main = val
        print(self.text_color_main)

    text_color_history = ImportSetting.save_data["history_tc"]
    def htc(self, val):
        self.text_color_history = val

    # Button press color
    press_color = ImportSetting.save_data["on_button_press_color"]
    # Font Size
    font_size_main = ImportSetting.save_data["fsm"]
    font_size_history = ImportSetting.save_data["fsh"]

    font_size_button_square = ImportSetting.save_data["fsbs"]
    font_size_button_top_bar = ImportSetting.save_data["fsbtb"]
    font_size_button_side_bar = ImportSetting.save_data["fsbsb"]

    def build(self):
        kv = Builder.load_file("main.kv")
        return kv


if __name__ == "__main__":
    MainApp().run()
    save_data = {'bcs': MainApp.button_color_square, 'bcs_t': MainApp.button_text_color_square,
                 'bct': MainApp.button_color_top_bar, 'bct_t': MainApp.button_text_color_top_bar,
                 'bcr': MainApp.button_color_side_bar, 'bcr_t': MainApp.button_text_color_side_bar,
                 "text_bc": MainApp.text_background_color, "button_bc": MainApp.button_background_color,
                 "history_bc": MainApp.history_background_color, "main_tc": MainApp.text_color_main,
                 "history_tc": MainApp.text_color_history, "on_button_press_color": MainApp.press_color,
                 "fsm": MainApp.font_size_main, "fsh": MainApp.font_size_history, "fsbs": MainApp.font_size_button_square,
                 "fsbtb": MainApp.font_size_button_top_bar, "fsbsb": MainApp.font_size_button_side_bar}

    with open('settings.txt', "w") as outfile:
        json.dump(save_data, outfile)

And then my kv file

                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "C"
                            on_press: root.clear()
                            font_size: dp(app.font_size_button_top_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_top_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_top_bar
                            background_color: app.button_color_top_bar
                        Button:
                            text: "+/-"
                            on_press: root.switch()
                            font_size: dp(app.font_size_button_top_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_top_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_top_bar
                            background_color: app.button_color_top_bar
                        Button:
                            text: "%"
                            on_press: root.percent()
                            font_size: dp(app.font_size_button_top_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_top_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_top_bar
                            background_color: app.button_color_top_bar
                        Button:
                            text: "÷"
                            on_press: root.divide()
                            font_size: dp(app.font_size_button_side_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_side_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_side_bar
                            background_color: app.button_color_side_bar

# Button Row 2
                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "7"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "8"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "9"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "x"
                            on_press: root.multiply()
                            font_size: dp(app.font_size_button_side_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_side_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_side_bar
                            background_color: app.button_color_side_bar

# Button Row 3
                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "4"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "5"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "6"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "-"
                            on_press: root.subtract()
                            font_size: dp(app.font_size_button_side_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_side_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_side_bar
                            background_color: app.button_color_side_bar

# Button Row 4
                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "1"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "2"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "3"
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "+"
                            on_press: root.add()
                            font_size: dp(app.font_size_button_side_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_side_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_side_bar
                            background_color: app.button_color_side_bar
# Button Row 5
                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "0"
                            size_hint: 2, 1
                            on_press: root.add_int(self)
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "."
                            on_press: root.decimal()
                            font_size: dp(app.font_size_button_square)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_square
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_square
                            background_color: app.button_color_square
                        Button:
                            text: "="
                            on_press: root.calculate()
                            font_size: dp(app.font_size_button_side_bar)
                            background_normal: ""
                            background_down: ""
                            color: app.button_text_color_side_bar
                            on_press: self.background_color = app.press_color
                            on_release: self.background_color = app.button_color_side_bar
                            background_color: app.button_color_side_bar

As you can see I created several variables in my main app that contain color data, I then assigned the color property's of my kivy buttons to the variables, then saved all the variable in a dictionary with json, then in the beginning of my code I create an import class and imported the dictionary, I then assigned each of my variables to the dictionary key that represents that variable. Now every time I launch my app the color setting save each time I close it, I verified this earlier by modifying one of the variable colors by re assigning a variable to a color an then ran the app, I then closed it and deleted the reassignment and ran again and the color was updated, however when I tried updating the color via a function at runtime the variable updated but not the button color. Just so you know I've just started learning python and kivy as well as programming in general and this is my first app.


Solution

  • Changing the value of a variable after the App is running will not affect the color of a widget even if you assigned the color of the widget from that variable. The color is set to the value of that variable when the kv is evaluated. The exception is if that variable is a Property. If you assign a widget color from a Property in the kv, then that widget color will change when that Property is changed. So, you just need to change your variables to Properties.