如何更新 kivy 标签颜色,即数据在运行时存储在主应用程序 class 的变量中

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

这是我的py文件

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)

然后是我的kv文件

                    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

如您所见,我在我的主应用程序中创建了几个包含颜色数据的变量,然后我将我的 kivy 按钮的颜色 属性 分配给变量,然后将所有变量保存在字典中使用 json,然后在我的代码开头创建一个导入 class 并导入字典,然后我将每个变量分配给代表该变量的字典键。现在每次我启动我的应用程序时,每次关闭它时都会保存颜色设置,我通过将变量重新分配给颜色然后 运行 应用程序来修改其中一种变量颜色来验证这一点,然后我关闭它并再次删除重新分配和 运行 并更新颜色,但是当我尝试在运行时通过函数更新颜色时,变量更新但不是按钮颜色。顺便提醒一下,我刚刚开始学习 python 和 kivy 以及一般编程,这是我的第一个应用程序。

App 之后更改变量的值 运行 不会影响小部件的颜色,即使您从该变量分配了小部件的颜色。评估 kv 时,颜色设置为该变量的值。例外情况是该变量是 Property。如果您从 kv 中的 Property 分配小部件颜色,则当 Property 更改时,该小部件颜色将更改。因此,您只需将变量更改为 Properties.