首页 > 解决方案 > 在 Python 3.8 中 - 如何从 RecycleView 中搜索我的小部件名称

问题描述

如何执行该search功能取决于我输入的内容textfield?如果我输入的文本textfield等于RecycleView data['name'],如何让那个唯一的小部件显示?

KV = '''

<Card>:
    AsyncImage:
        source: root.source

ScreenManager:
    id: screen_manager
    screen_manager: screen_manager
    
    Screen:
        name: 'main'
        
        BoxLayout:
            orientation: 'vertical'
            
            MDToolbar:
                title: "Card List"
                elevation: 10
                pos_hint: {'top': 1}
                
                MDIconButton:
                    icon: 'magnify'
                    pos_hint: {'center_y': .5}
                    theme_text_color: "Custom"
                    text_color: 1, 1, 1, 1
                    on_release: root.screen_manager.current = 'search'
    
            RecycleView:
                id: rv
                key_viewclass: 'viewclass'
                
                RecycleGridLayout:
                    cols: 3
                    padding: dp(17), dp(20)
                    spacing: dp(15)
                    size_hint_y: None
                    height: self.minimum_height
                    
                    default_size: dp(95), dp(135)
                    default_size_hint: None, None
                    orientation: 'vertical'

    Screen:
        name: 'search'
        
        BoxLayout:
            orientation: 'vertical'
            
            MDToolbar:
                title: "Search"
                elevation: 10
                pos_hint: {'top': 1}
                
                MDIconButton:
                    icon: 'arrow-left'
                    pos_hint: {'center_y': .5}
                    theme_text_color: "Custom"
                    text_color: 1, 1, 1, 1
                    on_release: root.screen_manager.current = root.screen_manager.previous()

            ScrollView:
                MDGridLayout:
                    cols: 1
                    padding: dp(20), dp(20)
                    spacing: dp(15)
                    size_hint_y: None
                    height: self.minimum_height
                    
                    MDTextField:
                        id: search_field
                        hint_text: "Search"
                        icon_right: 'magnify'

                    MDRaisedButton:
                        text: "Search"
                        on_release: app.pressed()
    
'''

class Card(MDCard):
    name = StringProperty()
    source = StringProperty()
    owner = ObjectProperty()

class Testing(MDApp):
    def build(self):
        return Builder.load_string(KV)

    def on_start(self):
        with open('images.json') as f:
            image_data = f.read()
            images = json.loads(image_data)['PRD'].items()

            self.root.ids.rv.data = [{'viewclass': "Card", 'name': k, 'source': v, 'owner': self} for k,v in images]

    def pressed(self):
        self.root.ids.search_field.text = ''

标签: pythonjsonkivykivymd

解决方案


推荐阅读