Kivymd中的背景

时间:2020-03-05 20:02:08

标签: python kivy kivy-language

from kivy.lang import Builder
from kivy.properties import StringProperty, BooleanProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen

from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior

# Your layouts.
Builder.load_string(
"""
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import Window kivy.core.window.Window
#:import IconLeftWidget kivymd.uix.list.IconLeftWidget
#:import toast kivymd.toast.toast


<ItemBackdropFrontLayer@TwoLineAvatarListItem>
    icon: "android"

    IconLeftWidget:
        icon: root.icon


<ItemBackdropBackLayer>
    size_hint_y: None
    height: self.minimum_height
    spacing: "10dp"

    canvas:
        Color:
            rgba:
                root.theme_cls.primary_dark if root.selected_item \
                else root.theme_cls.primary_color
        RoundedRectangle:
            pos: self.pos
            size: self.size

    MDIconButton:
        icon: root.icon
        theme_text_color: "Custom"
        text_color: 1, 1, 1, .5 if not root.selected_item else 1, 1, 1, 1

    MDLabel:
        text: ""
        color: 1, 1, 1, .5 if not root.selected_item else 1, 1, 1, 1

<ItemRoundBackdropBackLayerOfSecondScreen@BoxLayout>
    size_hint_y: None
    height: "40dp"
    spacing: "25dp"
    text: ""
    active: False
    group: ""

    MDCheckbox:
        group: root.group
        size_hint: None, None
        size: "30dp", "30dp"
        pos_hint: {"center_y": .5}
        selected_color: 1, 1, 1, 1
        active: root.active
        #on_active: app.printS(root.text)

    MDLabel:
        text: root.text
        color: 1, 1, 1, .7


<MyBackdropFrontLayer@ScrollView>
    backdrop: None
    backlayer: None

    GridLayout:
        size_hint_y: None
        height: self.minimum_height
        cols: 1
        padding: "5dp"

        ItemBackdropFrontLayer:
            text: "Sex"
            secondary_text: "SEX"
            icon: "monitor-star"
            on_press:
                root.backlayer.current = "sexo screen"
                root.backdrop.open()

<MyBackdropBackLayer@ScreenManager>
    transition: NoTransition()

    Screen:
        name: "sexo screen"
        id: "sexo screen"

        ScrollView

            GridLayout:
                size_hint_y: None
                height: self.minimum_height
                cols: 1
                padding: "15dp"
                spacing: "10dp"

                MDLabel:
                    text: "Selecione o seu sexo"
                    color: 1, 1, 1, 1

                Widget:
                    size_hint_y: None
                    height: "10dp"

                ItemRoundBackdropBackLayerOfSecondScreen:
                    text: "Male"
                ItemRoundBackdropBackLayerOfSecondScreen:
                    text: "Female"

"""
)

# Usage example of MDBackdrop.
Builder.load_string(
"""
<ExampleBackdrop>

    MDBackdrop:
        id: backdrop
        on_open: print("on_open")
        on_close: print("on_close")
        left_action_items: [['menu', lambda x: self.open()]]
        title: "Example Backdrop"
        header_text: "Menu:"

        MDBackdropBackLayer:
            MyBackdropBackLayer:
                id: backlayer

        MDBackdropFrontLayer:
            MyBackdropFrontLayer:
                backdrop: backdrop
                backlayer: backlayer
"""
)


class ExampleBackdrop(Screen):
    pass


class ItemBackdropBackLayer(ThemableBehavior, BoxLayout):
    icon = StringProperty("android")
    text = StringProperty()
    selected_item = BooleanProperty(False)

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):
            for item in self.parent.children:
                if item.selected_item:
                    item.selected_item = False
            self.selected_item = True
        return super().on_touch_down(touch)




class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.theme_cls.primary_palette = "DeepPurple"

    def build(self):
        return ExampleBackdrop()
    def printS(self,text):
        print(text)


Test().run()

我希望字幕在单击时出现。 即,根据所单击的内容,“辅助文本:” SEX””将是“男性”或“女性”。 它不一定必须在该变量“次要文本”中。我想到了将另一个带有相同信息的“ MDLabel”放在一边。 两种解决方案都是可行的。

enter image description here enter image description here

0 个答案:

没有答案