【问题标题】:Kivy Tabbed Panel won't change background colorKivy 选项卡式面板不会更改背景颜色
【发布时间】:2018-09-14 07:27:13
【问题描述】:

我有一个 kivy 应用程序,我可以在 python 文件中使用Window.clearcolor 创建一个白色背景的应用程序,正如kivy: change background color to white 中所建议的那样。然后我添加了一个选项卡式面板,这导致背景变回黑色。

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

带有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 背景作斗争。据我所知,我已经尝试过他们的建议。

直接相关的:

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案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'
    

    输出

    【讨论】:

    • 感谢您的回答。正如我在问题中提到的,我尝试使用 background_color 属性。我是用1,1,1,1设置成白色的,即使我试试你推荐的半透明蓝色,颜色还是不变。
    • @mdoc-2011:我已经更新了我的帖子,其中包含一个带有白色背景颜色的选项卡式面板内容示例。
    • 似乎'background_image:' 属性是必需的,即使我实际上并不想使用背景图像?
    【解决方案2】:

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

    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}
    

    【讨论】:

      猜你喜欢
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多