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

Kivy Tabbed Panel won't change background color

我有一个 kivy 应用程序,我可以使用 python 文件中的 Window.clearcolor 创建一个白色背景,如 kivy 中的建议:将背景颜色更改为白色的 。然后我添加了一个选项卡式面板,这导致背景变回黑色。

我尝试使用 canvascanvas.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'

屏幕截图:

添加面板之前:

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

尝试过

<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_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 背景上遇到过困难。据我所知,我已经尝试了他们的建议。

不太直接相关:

解决方案

使用提供的 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'

输出

我知道我回答晚了,但我在寻找另一个问题的答案时遇到了这个问题。这对某些人可能有帮助 :) 你可以做的是在盒子布局或网格布局中设置 canvas.before 就可以了,你不需要设置背景图像。 下面是如何使用它使我的面板背景变白的代码段。

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}