Kivy Tabbed Panel不会改变背景颜色

时间:2018-04-04 16:28:56

标签: python kivy kivy-language

我有一个kivy应用程序,我可以使用py Window.clearcolorkivy: change background color to white 中建议使用canvas创建白色背景。然后我添加了一个标签面板,这使得背景变回黑色。

我尝试使用canvas.beforebackground_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() 使其恢复为白色,但仍呈现黑色(或深蓝色)。

可重复的玩具示例

#: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'

但是当我向kv文件添加标签面板时,如下所示,背景显示为黑色(下面的屏幕截图):

<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: ...

SCREENSHOTS:

在添加面板之前:

window with white background

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

Panel with grey background

试过

<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:...

和类似的

TabbedPanel:
    do_default_tab: False

    TabbedPanelItem:
        text: 'Main'
        background_color: 1,1,1,1

如果我正确地阅读Kivy's documentation on TabbedPanels,我应该可以使用background_color,但这也不起作用:

TabbedPanel:
    do_default_tab: False
    background_color:1,1,1,1

    TabbedPanelItem:
        text: 'Main'

#include

相关:我知道其他人一直在与Kivy Backgrounds斗争。据我所知,我尝试过他们的建议。

不太直接相关:

2 个答案:

答案 0 :(得分:1)

解决方案

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

答案 1 :(得分:1)

我知道我来晚了,但是我在找到另一个问题的答案时遇到了这个问题。对某些人可能会有所帮助:) 您可以做的是设置画布。在采用框式布局或网格布局之前,可以完成此操作,而无需设置背景图像。 下面是该代码段如何使面板背景变白的方法。

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}
相关问题