【问题标题】:Changing the colour of a popup Kivy更改弹出 Kivy 的颜色
【发布时间】:2019-11-02 06:37:35
【问题描述】:

我的一个屏幕中有以下弹出窗口,我无法练习如何更改弹出窗口的背景颜色,它只是默认为 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()

【问题讨论】:

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


    【解决方案1】:

    如果您只满足于更改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()
    

    【讨论】:

    • 感谢回复,我试试看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多