如何在Kivy中更改TabbedPanelItem的背景颜色

时间:2019-03-26 14:16:18

标签: python kivy

我想更改选项卡上的背景颜色,并更改为默认的灰色,但是我无法找出方法。

我曾尝试定义一个画布,但最终在标签上像彩色图层一样创建了内容,然后我无法读取标签上的文本。

我的.py文件:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class IntroScreen(Screen):
    pass

class MainScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

app=Builder.load_file("main.kv")

class mainApp(App):
    def build(self):
        return app

mainApp().run()

main.kv文件:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import get_color_from_hex kivy.utils.get_color_from_hex

ScreenManagement:
    transition: FadeTransition()
    MainScreen:

<MainScreen>:
    name: "main_screen"

    BoxLayout:
        orientation: "horizontal"
        height: 30

        BoxLayout:
            orientation: "horizontal"

            TabbedPanel:
                do_default_tab: False
                tab_width: self.parent.width/2
                canvas.before:
                    Color:
                        rgb: get_color_from_hex("#ffffff")
                    Rectangle:
                        size: self.size
                        pos: self.pos

                TabbedPanelItem:
                    text: "Tab 1"
                    color: get_color_from_hex("#ffffff")
                    markup: True
                    Label:
                        text: "contents of tab 1"
                        color: get_color_from_hex("#000000")
                        canvas.before:
                            Color:
                                rgb: get_color_from_hex("#ffffff")
                            Rectangle:
                                size: self.size
                                pos: self.pos

                TabbedPanelItem:
                    text: "Tab 2"
                    markup: True
                    Label:
                        text: "contents of tab 2"
                        color: get_color_from_hex("#000000")
                        canvas.before:
                            Color:
                                rgb: get_color_from_hex("#ffffff")
                            Rectangle:
                                size: self.size
                                pos: self.pos

1 个答案:

答案 0 :(得分:0)

该选项卡是TabbedPanelHeader的类,它们具有以下属性,就像Button小部件一样。

示例

将以下内容添加到kv文件中:

<TabbedPanelHeader>:
    background_normal: 'blue.png'
    background_down: 'red.png'

输出

Tab1 Pressed Tab2 Pressed