简体   繁体   中英

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.

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.

In order to avoid annoying red points drawned on app screen when clicking on an element, modify the build method that way:

def build(self):
    # avoiding red dot put on Kivy screen after mouse right-click
    from kivy import Config
    Config.set('input', 'mouse', 'mouse,disable_multitouch')
    
    return kv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM