Search code examples
pythonuser-interfacekivyfilechooser

How to unselect multiple files selected in kivy filechooser List View


How can I clear the read only List Property selection of the kivy Filechooser or is there any workaround? The thing is that files stay selected even if I return back to my Filechooser window and this is really annoying.


Solution

  • Even though the selection of kivy filechooser is read-only, you can clear selection by just setting selection value to []

    Code Example:

    # import
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import Screen, ScreenManager
    
    
    class SM(ScreenManager):
        pass
    
    
    class Screen1(Screen):
        def selected(self, filename):
            # displaying seleted file name as a label
            self.ids.lb.text = str(filename)
    
    
    class Screen2(Screen):
        def deselect_action(self):
            # access screen1
            s1 = self.manager.get_screen('first')
            # resetting file selection
            s1.ids.select_file.selection = []
    
    # kivy file
    kv = Builder.load_string("""
    SM:
        Screen1:
        Screen2:
    
    <Screen1>:
        name: 'first'
    
        BoxLayout:
            orientation: 'vertical'
        
            FileChooserIconView:
                id: select_file
                on_selection: root.selected(select_file.selection)
            Label:
                id: lb
                text: 'default'
            Button:
                text: 'go to second screen'
                on_press: 
                    app.root.current = 'second'
    
    
    <Screen2>:
        name: 'second'
    
        Button:
            text: 'go back'
            on_press:
                app.root.current = "first"
                root.deselect_action()
    """)
    
    
    
    class filechoosing(App):
        def build(self):
            return kv
    
    filechoosing().run()
    

    Here you can see, the label takes value of the selected file. On returning to the Filechooser window screen, there are no selections.