为什么MDCard无法在KivyMD中添加on_touch_down?

我已经实施了以下。底部有两个按钮。单击第一个按钮时,它会从主屏幕转到第一个屏幕。在第一个屏幕上,单击右上角按钮将创建 10 张卡片。

但是当尝试添加on_touch_down时它会崩溃。我添加了toast print一些功能,但没有任何效果。

那么这个问题的解决方案是什么?谢谢

main.py 我已经评论过on_touch_down = toast('clicked')。没有它它也可以工作,但是当取消注释时,应用程序就会崩溃。如何实施?这样该卡就可以点击了。

build_string.py


helper_string = """

<MenuButton@MDIconButton>:

    icon: "menu"

    theme_text_color: "Custom"

    text_color: 1,0,0,1

    halign: 'bottom'  

    on_press: app.show_main_grid_bottom_sheet()


<TButton@MDIconButton>:

    icon: "menu"

    theme_text_color: "Custom"

    text_color: 1,0,0,1

    halign: 'center'  


<TitleText@MDLabel>

    pos_hint: {"center_y": .95}

    halign: "center"

    theme_text_color: "Custom"

    text_color: 0, 0, 1, 1

    font_style: "Subtitle1"


ScreenManager:

    id: screen_manager

    MainScreen:

    FirstScreen:

        id: first_screen_id

    SecondScreen:


<MainScreen>:

    name: 'main_screen'

    BoxLayout:

        orientation: "vertical"

        spacing: "5dp"


        MDToolbar:

            id: toolbar

            pos_hint: {'bottom': 1}

            #right_action_items: [["dots-vertical", lambda x: app.show_main_grid_bottom_sheet()]]

            left_action_items: [["menu", lambda x: app.show_main_grid_bottom_sheet()]]   


<FirstScreen>:

    name: 'first_screen'

    BoxLayout:

        id: first_screen_box

        orientation: "vertical"

        spacing: "5dp"


        MDToolbar:

            id: toolbar

            title: "First Screen"

            elevation: 5

            pos_hint: {'top': 1}

            right_action_items: [["refresh", lambda x: app.make_card()]]


        ScrollView:                

            MDBoxLayout:

                id: first_screen_box_layout

                orientation: 'vertical'

                adaptive_height: True

                padding: dp(15)

                spacing: dp(5) 




莫回无
浏览 97回答 1
1回答

慕尼黑的夜晚无繁华

是on_touch_down您可以绑定的事件,它不是MDCard您可以设置的属性。所以你可以像这样进行绑定:def make_card(self):&nbsp; &nbsp; for i in range(10):&nbsp; &nbsp; &nbsp; &nbsp; card = MDCard(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orientation="vertical",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding="8dp",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ripple_behavior=True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size_hint=[1, None],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #on_touch_down = toast('clicked')&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; card.bind(on_touch_down=self.clicked)&nbsp; # set binding&nbsp; &nbsp; &nbsp; &nbsp; label_link = MDLabel(text="Card" + str(i))&nbsp; &nbsp; &nbsp; &nbsp; label_link.font_style = "Caption"&nbsp; &nbsp; &nbsp; &nbsp; label_header = MDLabel(text="Title" + str(i))&nbsp; &nbsp; &nbsp; &nbsp; label_header.size_hint = [1, 1]&nbsp; &nbsp; &nbsp; &nbsp; card.add_widget(label_link)&nbsp; &nbsp; &nbsp; &nbsp; card.add_widget(label_header)&nbsp; &nbsp; &nbsp; &nbsp; self.sm.ids.first_screen_id.ids.first_screen_box_layout.add_widget(card)# method called by bindingdef clicked(self, card, touch):&nbsp; &nbsp; if card.collide_point(*touch.pos):&nbsp; &nbsp; &nbsp; &nbsp; print('clicked on', card.children[0].text)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python