Kivy:使用 build() 方法创建彩色标签列表

Kivy: Create a list of coloured labels using build() method

我想创建一个彩色标签列表。问题是我可以用 kv 文件来做,但我需要通过 build() 方法来做。所以我尝试复制我所做的,但它不起作用。我不明白为什么。

这是我编写的代码

from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import *

class RL(RelativeLayout): # Creates the background colour for each label
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        with self.canvas:
            Color(.7, 0, .5, 1)
            Rectangle(size_hint=self.size)

class MainMenu(BoxLayout):
    
    N_LBLS = 8
    labels_text = []
    RL_list = []

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        button = Button(text='do something')
        button.bind(on_release=self.change_text)
        box = BoxLayout(orientation='vertical', padding= 10, spacing = 15)
        for i in range(0, self.N_LBLS):
            self.RL_list.append(RL())
            self.labels_text.append(Label(text=f'{i}º label', size_hint=self.size))
            self.RL_list[i].add_widget(self.labels_text[i])
            box.add_widget(self.RL_list[i])
        self.add_widget(button)
        self.add_widget(box)

    def change_text(self, instance):
        for lbl in self.labels_text:
            if lbl.text[0] == '5':
                lbl.text = 'Text changed'

class MainApp(App):
    def build(self):
        return MainMenu()

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

它应该在左边做一个按钮,在右边做一个包含 8 个彩色标签的列表。

问题是您在每个标签中设置了 size_hint=self.sizeself.sizeMainMenu 的大小,执行该代码时为 [100,100]。请注意,size_hint 是应用于父级大小以计算小部件大小的乘数。因此 [100,100]size_hint 使每个 LabelMainMenu 大 100 倍。所以您的代码可以正常工作,但是 Labels 太大以至于文本不在屏幕上。首先删除 size_hint=self.size.

而且,要在 Label 上设置背景颜色,您可以只使用 Labelcanvas,而不是某些容器。这是执行此操作的代码版本:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


class ColorLabel(Label):
    pass


Builder.load_string('''
<ColorLabel>:
    bg_color: [.7, 0, .5, 1]
    canvas.before:
        Color:
            rgba: self.bg_color
        Rectangle:
            pos: self.pos
            size: self.size
''')


class MainMenu(BoxLayout):
    N_LBLS = 8
    labels_text = []

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        button = Button(text='do something')
        button.bind(on_release=self.change_text)
        box = BoxLayout(orientation='vertical', padding=10, spacing=15)
        for i in range(0, self.N_LBLS):
            self.labels_text.append(ColorLabel(text=f'{i}º label'))
            box.add_widget(self.labels_text[i])
        self.add_widget(button)
        self.add_widget(box)

    def change_text(self, instance):
        for lbl in self.labels_text:
            if lbl.text[0] == '5':
                lbl.text = 'Text changed'
                lbl.bg_color = [0, 1, 0, 1]


class MainApp(App):
    def build(self):
        return MainMenu()


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