如何创建一个 python 颜色存储在变量中的 kivy 标签,该变量可以被其他 kivy 小部件 类 访问并在运行时更新

How to create a python kivy label that's color is stored in a variable that can be accessed by other kivy widget classes and updated during runtime

我有三个 .kv 文件,每个文件对应我应用程序中的一个屏幕,一个是屏幕管理器,另外两个是实际屏幕。我需要将我的一个屏幕中标签的颜色属性存储到我的主应用程序 class 中的变量,这样我就可以从我的另一个屏幕更新该变量并更改另一个屏幕中标签的颜色.

main.py

from utils.imports import *
Window.size = (400, 700)


class ScreenM(ScreenManager):
    pass

class Calc(Screen):
# there are a bunch of functions in here to do calculations but i felt there wasn't a need to include them

class MenuScreen(Screen):
    pass

class Main(App):
    color = rgba("#FFF000")
    def update_color(self):
        self.color = rgba("#000000")
    def build(self):
        return ScreenM()


if __name__ == "__main__":
    Main().run()

main.kv

#:include calc.kv
#:include menu_screen.kv
<ScreenM>:
    Calc:
    MenuScreen:

calc.kv

#:include utils/custom_widgets.kv
<Calc>:
    name: "calc"
    #:set sp dp("5")
    AnchorLayout:
        padding: sp
# Text background Color
        LabelB:
            bcolor: 1,1,1,1

        BoxLayout:
            orientation: "vertical"
            BoxLayout:
# Text Panel
                AnchorLayout:
                    size_hint: 1, 1
                    BoxLayout:
                        padding: sp
                        orientation: "vertical"
                        AnchorLayout:
                            anchor_y: "top"
                            size_hint: .15, .15
                            LabelB:
                                bcolor: 1, 1, 1, 1
                            ButtonI:
                                source: "resources/images/square_menu_icon.resized.png"
                            Button:
                                on_release: app.root.current = "menu"
                                background_normal: "resources/images/empty.png"
                                background_down: "resources/images/gray_n.jpeg"
# The Label i want to chage the color of
#############
#############

                        Label:
                            text: root.d1
                            font_size: 50 if self.text == '' else min(48,max(30/len(self.text) * 15, 15))
                            color: app.color
#############
#############
#############
# History Panel
                AnchorLayout:
                    padding: sp
                    size_hint: .335, 1
# History Background Color
                    LabelB:
                        bcolor: 1, 1, 1, 1
                    ScrollView:
                        start_pos: 'bottom'
                        padding: 5
                        Label:
                            text: root.history
                            text_size: self.width, None
                            color: 1, 0, 0, 1
                            font_size: 40
                            halign: 'left'
                            valign: 'top'
                            size_hint_y: None
                            height: self.texture_size[1]
# Buttons
            AnchorLayout:
# Buttons Background Color
                LabelB:
                    bcolor: 1, 1, 1, 1
# Main Layout
                BoxLayout:
                    orientation: "vertical"
                    spacing: sp

# Button Row 1
                    BoxLayout:
                        spacing: sp
                        Button:
                            text: "C"
                            on_press: app.update_color()
                            font_size: dp(20)

# i remove the rest of the buttons for simplicity

menu_screen.kv

<MenuScreen>:
    name: "menu"
    Button:
        text: "change color"
        on_press: app.update_color
        on_press: app.root.current = "calc"

当我 运行 颜色没有任何变化时,我们将不胜感激。

只需更改:

class Main(App):
    color = rgba("#FFF000")

至:

class Main(App):
    color = ListProperty(rgba("#FFF000"))

当在 kv 中设置属性(如 color)时,它们在计算 kv 时采用变量的值。如果你想让widget的颜色随着变量的变化而变化,变量必须是Property.