如果您只满足于更改Popup 的Label 部分的背景颜色,您可以定义自己的Label 子类:
class MyLabel(Label):
pass
在你的“kv”中:
<MyLabel>:
canvas.before:
Color:
rgba: 1,0,0,1
Rectangle:
pos: self.pos
size: self.size
然后,在您的Popup 中使用MyLabel 而不是Label 将为您提供红色背景(但不适用于Popup 的标题区域)。
如果您想更改整个Popup 的背景颜色,我认为您需要重新定义Popup 的预定义样式。同样,创建Popup 的子类:
class MyPopup(Popup):
bg_color = ListProperty([0,0,0,1])
bg_color 将成为背景颜色。
现在重新定义样式:
<-MyPopup>:
_container: container
GridLayout:
padding: '12dp'
cols: 1
size_hint: None, None
pos: root.pos
size: root.size
Label:
canvas.before:
Color:
rgba: root.bg_color
Rectangle:
pos: self.pos
size: self.size
text: root.title
color: root.title_color
size_hint_y: None
height: self.texture_size[1] + dp(16)
text_size: self.width - dp(16), None
font_size: root.title_size
font_name: root.title_font
halign: root.title_align
Widget:
size_hint_y: None
height: dp(4)
canvas.before:
Color:
rgba: root.bg_color
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: root.separator_color
Rectangle:
pos: self.x, self.y + root.separator_height / 2.
size: self.width, root.separator_height
BoxLayout:
canvas.before:
Color:
rgba: root.bg_color
Rectangle:
pos: self.pos
size: self.size
id: container
以上kv开头的-,表示我们正在重新定义默认样式(以上kv大部分都是从默认style.kv复制而来)。使用canvas.before 设置背景颜色。 MyPopup 现在有一个 bg_color 属性,您可以将其设置为您想要的任何颜色,例如,将背景设置为红色:
def none_selected(self):
pop = MyPopup(title='Error',
content=Label(text='Please select at least one option', multiline=True,),
size_hint=(None, None), size=(250, 200), bg_color=[1,0,0,1])
pop.open()