Kivy定义标签的背景颜色

Kivy define background color of label

我在 Python 的 Kivy 语言中为各种对象(在本例中为标签)定义背景颜色时遇到了问题。附加代码创建了一个框布局,其中每个框都有特定的背景颜色。

  1. 如何调整此代码,以便我可以通过变量定义特定标签的大小,而不是对其进行硬编码。如果我们能想出这样的解决方案,那就最好了:
    ForeignLanguage = ColoredLabel(..., size = (a,b))
  1. 我的代码看起来很复杂。是否可以以更简单的方式设置标签的背景颜色,例如 Label(..., background_color = (1,1,1,1))?

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label
    from kivy.properties import ListProperty
    from kivy.lang import Builder
    from kivy.core.window import Window


    kv = '''
    <ColoredLabel>:
        size: (self.size_x,self.size_y)
        pos: (0,0) # no effect
        background_color:
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                pos: self.pos
                size: (self.size_x,self.size_y)
        '''

    Builder.load_string(kv)

    class ColoredLabel(Label):
        background_color = ListProperty((0,0,0,1))
        s_global = Window.size
        size_x = s_global[0]
        size_y = s_global[1]/3

    class MyWidget(BoxLayout):
        #init
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            ForeignLanguage = ColoredLabel(text="ForeignLanguage", size_hint=(None, None), background_color=(0/255,171/255,169/255, 1))
            Translation = ColoredLabel(text="Translation", size_hint=(None, None), background_color=(45/255,137/255,239/255, 1))
            Example = ColoredLabel(text="Example", size_hint=(None, None), background_color=(43/255,87/255,151/255, 1))
            verticalBox     = BoxLayout(orientation='vertical')
            verticalBox.add_widget(ForeignLanguage)
            verticalBox.add_widget(Translation)
            verticalBox.add_widget(Example)
            self.add_widget(verticalBox)

    class BoxLayoutDemo(App):
        def build(self):
            return MyWidget()

    if __name__ == '__main__':
        BoxLayoutDemo().run()

问题 1

How can this code be adapted so that I can define the size of the certain label via a variable, rather than hard code it. Best would be if we could come up with a solution like this:

ForeignLanguage = ColoredLabel(..., size = (a,b))

回答

只需在您的 kv 文件中进行以下设计。通过 Label 小部件的继承,定义的所有属性都将可用,例如sizesize_hintcolormarkup

片段 - kv 文件

<ColoredLabel>:
    canvas.before:
        Color:
            rgba: root.background_color
        Rectangle:
            pos: self.pos
            size: self.size

问题 2

My code appears quite complicated to me. Is it possible to set the background color of a label in a more easy way, like Label(..., background_color = (1,1,1,1))?

回答

您的代码看起来不错。

片段 - py 文件

    ForeignLanguage = ColoredLabel(text="ForeignLanguage", size_hint=(None, None), size=(300, 40),
                                   background_color=(1, 0, 0, 1))
    Translation = ColoredLabel(text="Translation", size_hint=(None, None), size=(100, 30),
                               background_color=(0, 1, 0, 1))
    Example = ColoredLabel(text="Example", size_hint=(None, None), size=(200, 20),
                           background_color=(0, 0, 1,  1))

输出