我在其中一个屏幕中具有以下弹出窗口,并且我无法锻炼如何更改弹出窗口的背景颜色,它仅默认为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()
答案 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