更改弹出窗口Kivy的颜色



我的一个屏幕上有下面的弹出窗口,我无法训练如何更改弹出窗口的背景色,它只是默认为Kivy标准灰色。我试过background_color,但它改变了后面的整个屏幕。

def none_selected(self):
pop = Popup(title='Error',
content=Label(text='Please select at least one option', multiline=True,),
size_hint=(None, None), size=(250, 200))
pop.open()

如果您只想更改PopupLabel部分的背景色,那么您可以定义自己的Label子类:

class MyLabel(Label):
pass

在你的"千伏":

<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()

最新更新