在动画背景中显示的卡片(KivyMD)(如何摆脱它)

时间:2021-01-10 13:15:59

标签: python kivy kivymd

好的,我想为我的应用中的卡片制作动画。因为将所有字段和所有内容都放在一张卡片中会变得混乱。我想避免更改屏幕,而只是为卡片本身设置动画。该应用程序是“注册用户” - 某种东西。但是当我尝试为卡片设置动画时,第一张卡片 (card_1) 在动画期间潜伏在所有内容的后面。或者更确切地说是一份不透明度较低的副本......不明白他为什么不离开。

图片:

Card in background

.kv 文件:

<RegisterUser>:
    name: "registeruser"
    BoxLayout:
        canvas.before:
            Color:
                rgba: 0, 0.3, 0.6, 0.4
            Rectangle:
                size: self.size
                pos: self.pos
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: "Mauri"
            right_action_items: [["account", lambda x: root.goto_userlogin()]]
        BoxLayout:
    FloatLayout:
        MDCard:
            id: card_2
            orientation: 'vertical'
            size_hint: [0.9, 0.6]
            pos_hint: {"center_x": 1.5, "center_y": 0.5}
            BoxLayout:
                orientation: 'vertical'
                size_hint: [1, 0.1]
                MDLabel:
                    halign: "center"
                    text: "Registrera 2"
                    color: 0, 0, 0, 0.8
                    font_size: "25dp"
                    bold: True
                MDLabel:
                    halign: "center"
                    text: "Användare"
                    color: 0, 0.1, 0.3, 0.8
                    font_size: "15dp"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDTextField:
                    id: email
                    hint_text: 'Namn'
                    helper_text_mode: "on_error"
                    helper_text: "Email Krävs"
                    pos_hint: {"center_y": 0.5}
                    mode: "rectangle"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDTextField:
                    id: email
                    hint_text: 'Efternamn'
                    helper_text_mode: "on_error"
                    helper_text: "Email Krävs"
                    pos_hint: {"center_y": 0.5}
                    mode: "rectangle"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDRaisedButton:
                    id: btn_institution
                    text: "Välj institution"
                    on_release: root.show_institutions()
                    pos_hint: {"center_x": .5, "center_y": .5}
                MDLabel:
                    id: inst_label
                    color: (0, 0, 0, 1)
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDTextField:
                    id: pwd
                    icon_left: 'key-variant'
                    hint_text: 'Ålder'
                    mode: "rectangle"
                    max_text_length: 2
                    input_filter: "int"
                    pos_hint: {"center_y": 0.5}
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDRaisedButton:
                    id: btn_sexes
                    text: "Välj Kön"
                    on_release: root.show_sexes()
                    pos_hint: {"center_x": .5, "center_y": .5}
                MDLabel:
                    id: sex_label
                    color: (0, 0, 0, 1)
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDFillRoundFlatButton:
                    pos_hint: {"center_x": 0.5}
                    size_hint: [0.7, 0.5]
                    text: "Nästa"
                    font_size: "20dp"

        MDCard:
            id: card_1
            orientation: 'vertical'
            size_hint: [0.9, 0.6]
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            BoxLayout:
                orientation: 'vertical'
                size_hint: [1, 0.1]
                MDLabel:
                    halign: "center"
                    text: "Registrera"
                    color: 0, 0, 0, 0.8
                    font_size: "25dp"
                    bold: True
                MDLabel:
                    halign: "center"
                    text: "Användare"
                    color: 0, 0.1, 0.3, 0.8
                    font_size: "15dp"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDTextField:
                    id: email
                    hint_text: 'Namn'
                    helper_text_mode: "on_error"
                    helper_text: "Email Krävs"
                    pos_hint: {"center_y": 0.5}
                    mode: "rectangle"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDTextField:
                    id: email
                    hint_text: 'Efternamn'
                    helper_text_mode: "on_error"
                    helper_text: "Email Krävs"
                    pos_hint: {"center_y": 0.5}
                    mode: "rectangle"
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDRaisedButton:
                    id: btn_institution
                    text: "Välj institution"
                    on_release: root.show_institutions()
                    pos_hint: {"center_x": .5, "center_y": .5}
                MDLabel:
                    id: inst_label
                    color: (0, 0, 0, 1)
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDTextField:
                    id: pwd
                    icon_left: 'key-variant'
                    hint_text: 'Ålder'
                    mode: "rectangle"
                    max_text_length: 2
                    input_filter: "int"
                    pos_hint: {"center_y": 0.5}
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                spacing: "20dp"
                MDRaisedButton:
                    id: btn_sexes
                    text: "Välj Kön"
                    on_release: root.show_sexes()
                    pos_hint: {"center_x": .5, "center_y": .5}
                MDLabel:
                    id: sex_label
                    color: (0, 0, 0, 1)
            BoxLayout:
                size_hint: [0.8, 0.1]
                pos_hint: {"center_x": 0.5}
                MDFillRoundFlatButton:
                    pos_hint: {"center_x": 0.5}
                    size_hint: [0.7, 0.5]
                    text: "Nästa"
                    font_size: "20dp"
                    on_release:
                        root.anim_to_next(card_1, card_2)
            BoxLayout:
                size_hint: [0.4, 0.1]

.py 文件:

class RegisterUser(Screen):

    def on_enter(self):
        self.sql_cursor = app_users.AppUsers()
        self.all_inst = self.sql_cursor.fetch_all_institutions()
        self.selected_inst = -1
        self.selected_sex = -1


    def callback_for_inst_items(self, *args):
        toast(args[0])
        self.ids["inst_label"].text = args[0]
        self.selected_inst = args[1]
        print(self.selected_inst)


    def show_institutions(self):
        bottom_sheet_menu = MDListBottomSheet()
        for i in range(len(self.all_inst)):
            bottom_sheet_menu.add_item(
                self.all_inst[i][1].capitalize() + " (" + self.all_inst[i][2] + ")",
                lambda x, y=i: self.callback_for_inst_items(
                    self.all_inst[y][1].capitalize() + " (" + self.all_inst[y][2] + ")", self.all_inst[y][0]
                ),
            )
        bottom_sheet_menu.open()

    def callback_for_sexes_items(self, *args):
        self.ids["sex_label"].text = args[0]
        self.selected_sex = args[1]
        print(self.selected_sex)


    def show_sexes(self):
        bottom_sheet_menu = MDListBottomSheet()
        sex = ["Man", "Kvinna", "Annat"]
        for i in range(3):
            bottom_sheet_menu.add_item(
                sex[i],
                lambda x, y=i: self.callback_for_sexes_items(
                    sex[y], y
                ),
            )
        bottom_sheet_menu.open()


    def anim_to_next(self, card_1, card_2):
        anim_1_1 = Animation(opacity=0,duration=0.5)
        anim_1 = Animation(pos_hint={"center_x": -0.5},duration=0.7)

        anim_2 = Animation(pos_hint={"center_x": 0.5})
        anim_1_1.start(card_1)
        anim_1.start(card_1)
        anim_2.start(card_2)

问题:

如何摆脱潜伏在后台的“第三个”(即 ID 为“card_1”的卡片)?为什么会在那里?

1 个答案:

答案 0 :(得分:0)

一种解决方案:

.kv-file 中,我将 canvas.before Rectangle 的不透明度更改为 1:

    canvas.before:
        Color:
            rgba: 0, 0.3, 0.6, 1
        Rectangle:
            size: self.size
            pos: self.pos

然而,我仍然不知道为什么或如何发生这种情况。

John Anderson 提供的解决方案

“由于您无法提供最小的可重现示例,您可能会检查您是否无意中两次加载了 kv 文件。如果您根据 kivy 的自动加载功能命名 kv 文件,并且还加载了 kv 文件,则会发生这种情况显式使用 Builder 的文件。”