更改弹出式Kivy的颜色

时间:2019-11-01 21:42:03

标签: python-3.x kivy kivy-language

我在其中一个屏幕中具有以下弹出窗口,并且我无法锻炼如何更改弹出窗口的背景颜色,它仅默认为Kivy标准灰色。我尝试了background_colour,但是它改变了整个屏幕。

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

1 个答案:

答案 0 :(得分:0)

如果您只想更改Label的{​​{1}}部分的背景颜色,则可以定义自己的Popup子类:

Label

和您的“ kv”中

class MyLabel(Label):
    pass

然后,在您的<MyLabel>: canvas.before: Color: rgba: 1,0,0,1 Rectangle: pos: self.pos size: self.size 中使用MyLabel而不是Label将为您提供红色背景(但不适用于Popup的标题区域)。

如果您想更改整个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