1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
'''
Example of an Android filechooser.
'''
from textwrap import dedent
from plyer import filechooser
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.button import Button
class FileChoose(Button):
'''
Button that triggers 'filechooser.open_file()' and processes
the data response from filechooser Activity.
'''
selection = ListProperty([])
def choose(self):
'''
Call plyer filechooser API to run a filechooser Activity.
'''
filechooser.open_file(on_selection=self.handle_selection)
def handle_selection(self, selection):
'''
Callback function for handling the selection response from Activity.
'''
self.selection = selection
def on_selection(self, *a, **k):
'''
Update TextInput.text after FileChoose.selection is changed
via FileChoose.handle_selection.
'''
App.get_running_app().root.ids.result.text = str(self.selection)
class ChooserApp(App):
'''
Application class with root built in KV.
'''
def build(self):
return Builder.load_string(dedent('''
<FileChoose>:
BoxLayout:
BoxLayout:
orientation: 'vertical'
TextInput:
id: result
text: ''
hint_text: 'selected path'
FileChoose:
size_hint_y: 0.1
on_release: self.choose()
text: 'Select a file'
'''))
if __name__ == '__main__':
ChooserApp().run()
|