【问题标题】:Card showing in the background on animation (KivyMD) (How to get rid of it)动画背景中显示的卡片(KivyMD)(如何摆脱它)
【发布时间】:2021-01-10 13:15:59
【问题描述】:

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

图片:

.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”的卡片)?为什么会在那里?

【问题讨论】:

  • 似乎无法重现该问题。当我尝试时一切正常。
  • 我建议使用 Clock.schedule_interval() 手动为位置设置动画,并在您不需要时完全删除小部件
  • 好的,所以如果我在 canvas.before 中更改填充矩形的不透明度,框会被绘制,这可能是一个临时解决方案,但我宁愿希望它“消失”。
  • 由于您无法提供minimal reproducible example,您可能会检查您是否无意中加载了您的kv 文件两次。如果您根据 kivy 的自动加载功能命名 kv 文件,并使用 Builder 显式加载文件,则会发生这种情况。

标签: python kivy kivymd


【解决方案1】:

一种解决方案:

.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 文件,并同时加载该文件显式使用 Builder。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2021-04-19
    • 1970-01-01
    • 2013-12-12
    • 2020-09-24
    • 1970-01-01
    • 2017-11-07
    相关资源
    最近更新 更多