代码之家  ›  专栏  ›  技术社区  ›  mdoc-2011

Kivy选项卡式面板不会更改背景颜色

  •  0
  • mdoc-2011  · 技术社区  · 6 年前

    我有一个kivy应用程序,我可以使用 Window.clearcolor 在python文件中,如中所建议 kivy: change background color to white 。然后我添加了一个选项卡式面板,使背景恢复为黑色。

    我试图使用 canvas canvas.before background_color 使其恢复为白色,但仍呈现黑色(或者更确切地说是深灰色)。

    可复制玩具示例

    import kivy
    from kivy.lang import Builder
    from kivy.core.window import Window
    
    
    kivy.require('1.1.0')
    
    from kivy.app import App
    
    presentation = Builder.load_file("works.kv")
    class TestApp(App):
        def build(self):
            Window.clearcolor = (1, 1, 1, 1)
            return presentation
    
    
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    使用kv文件:

    #:kivy 1.10.0
    GridLayout:
        cols: 2
    
        Label:
            text:'just to force spacing'
        Button:
            text: 'Hello World'
    

    但是,当我向kv文件添加选项卡式面板时,如下图所示,背景看起来是黑色的(下面的屏幕截图):

    #:kivy 1.10.0
    BoxLayout:
        TabbedPanel:
            do_default_tab: False
            background_color: (1, 1, 1, 1)
    
            TabbedPanelItem:
                text: 'Main'
    
                GridLayout:
                    cols: 2
    
                    Label:
                        text:'just to force spacing'
                    Button:
                        text: 'Hello World'
    
            TabbedPanelItem:
                text: 'Tab 2'
    

    屏幕截图:

    添加面板之前:

    window with white background

    添加面板后(我希望面板有一个白色背景,在这个玩具示例中,文本是白底白字,但我在我的应用程序中处理过):

    Panel with grey background

    尝试

    <Main>:
        name: 'mscreen'
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
    
        TabbedPanel:
            do_default_tab: False
    
            TabbedPanelItem:
                text: 'Main'
    
                GridLayout: ...
    

    同样地

    <Main>:
        name: 'mscreen'
        canvas:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
    
        TabbedPanel:
            do_default_tab: False
    
            TabbedPanelItem:
                text: 'Main'
    
                GridLayout:...
    

    如果我在看书 Kivy's documentation on TabbedPanels 正确地说,我应该能够使用background\u color,但这也不起作用:

    TabbedPanel:
        do_default_tab: False
    
        TabbedPanelItem:
            text: 'Main'
            background_color: 1,1,1,1
    

    TabbedPanel:
        do_default_tab: False
        background_color:1,1,1,1
    
        TabbedPanelItem:
            text: 'Main'
    

    相关的 :我知道其他人都在与Kivy的背景作斗争。据我所知,我尝试了他们的建议。

    不太直接相关:

    2 回复  |  直到 6 年前
        1
  •  1
  •   ikolim    6 年前

    解决方案

    使用提供的kv文件和一些补充。

    kv文件-白色选项卡式面板内容

    #:kivy 1.10.0
    BoxLayout:
        TabbedPanel:
            do_default_tab: False
            background_color: (1, 1, 1, 1)    # White colour
            border: [0, 0, 0, 0]
            background_image: 'path/to/background/image'
    
            TabbedPanelItem:
                text: 'Main'
    
                GridLayout:
                    cols: 2
    
                    Label:
                        text:'just to force spacing'
                    Button:
                        text: 'Hello World'
    
            TabbedPanelItem:
                text: 'Tab 2'
    

    蓝色选项卡式面板内容

    要更改主选项卡式面板内容的外观,请执行以下操作:

    TabbedPanel:
        background_color: (0, 0, 1, .5)    # 50% translucent blue
        border: [0, 0, 0, 0]
        background_image: 'path/to/background/image'
    

    输出

    Img01 - White Tabbed Panel content Img02 - Blue Tabbed Panel content

        2
  •  1
  •   Rohan Malodia    4 年前

    我知道我回答得晚了,但我在找到另一个问题的答案时遇到了这个问题。这可能对某些人有帮助:) 您可以做的是设置画布。在框内布局或网格布局完成之前,不需要设置背景图像。 下面是一段代码,它是如何使我的面板背景变为白色的。

    TabbedPanel:
    
        do_default_tab: False
    
        TabbedPanelItem:
           # This will change the tab panel button color
           background_color: 0.0,0.9,2,1
           text: 'Scripts'
    
            BoxLayout :
                   # This will change the background to white
                    canvas.before:
                        Color:
                            rgba:1,1,1,1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    orientation: "vertical"
                    # Recycle view I've used to show the list 
                    RV:
                        id: listrecycleview
                        pos_hint:{"x":0, "y": 0.1}