1

Could somebody post a small working example of a kivy Filechooser with the following simple doubleclick function: doubleclicking on a file will print out the filename?

picibucor
  • 753
  • 1
  • 8
  • 25

2 Answers2

4

Here is an example of that.

from kivy.app import App
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.boxlayout import BoxLayout


class MyFileChooser(FileChooserListView):

    def on_submit(*args):
        print(args[1][0])


class MyLayout(BoxLayout):

    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        # filter added. Since windows will throw error on sys files
        self.fclv = MyFileChooser(filters= [lambda folder, filename: not filename.endswith('.sys')])
        self.add_widget(self.fclv)


class MyApp(App):

    def build(self):
        return MyLayout()


MyApp().run()
el3ien
  • 5,362
  • 1
  • 17
  • 33
  • which version of Kivy are you using? – picibucor Feb 23 '17 at 10:38
  • @picibucor v1.9.1. Sry I forgot to print the filename :D I will update – el3ien Feb 23 '17 at 11:29
  • your code does not work for me. I am using the newest nightly build (2017.02.22) – picibucor Feb 23 '17 at 12:02
  • @picibucor okey, it should still work tho.. Any errors? – el3ien Feb 23 '17 at 12:14
  • @picibucor try take the `path='.'` out of `MyFileChooser`. There were some issues around that, at that date. – el3ien Feb 23 '17 at 12:17
  • pywintypes.error: (32, 'GetFileAttributesEx', 'Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird.') – picibucor Feb 23 '17 at 13:17
  • @picibucor I dont know what language that is. But seems to me that the file you doubleclick is used by another process, and that is why you cannot get the data. – el3ien Feb 23 '17 at 13:19
  • pywintypes.error: (32, 'GetFileAttributesEx', The process cannot be accessed because another process uses it.) – picibucor Feb 23 '17 at 13:33
  • Sorry, it starts with: File "C:\Python34\lib\site-packages\kivy\uix\filechooser.py", line 178, in is_hidden return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN – picibucor Feb 23 '17 at 13:39
  • @picibucor https://github.com/kivy/kivy/blob/master/kivy/uix/filechooser.py#L178 yes so as it says under that error: This error can occurred when a file is already accessed by someone else. – el3ien Feb 23 '17 at 14:12
  • @picibucor that said, I am on linux, and have tried open a .jpg file for example, and then try to double click in the kivy app, and still works here. I have a windows pc, that I will try this in, in a moment. – el3ien Feb 23 '17 at 14:14
  • @picibucor yes it works. Navigate to a file, doubleclick, and it will print. Alltho the same error as you mention pops up if I am in the directory where the python file which I run is. So you can ignore that error – el3ien Feb 24 '17 at 09:47
  • @picibucor actually the problem is sys files in windows. To get rid of it, you can exclude sys files with a filter. I will add the filter in my answer. – el3ien Feb 24 '17 at 10:02
  • The filter works like a charm, there is no error. But the double-click is not working. – picibucor Feb 28 '17 at 10:41
  • I've also installed kivy 1.9.2 , maybe it's the problem is in the dev-branch but nothing changed. – picibucor Feb 28 '17 at 10:42
  • @picibucor weird. I tested it on windows, also newly installed, and it prints the filename. Tho only if it is not a directory. It will only open the directory. But file names print – el3ien Feb 28 '17 at 10:50
2

I think it is simpler than that.

FileChooser has an argument dirselect. By default it is False making it single-click. If you change dirselect to True, it works as double-click.

For example, in kivy language

BoxLayout:
    FileChooserIconView:
        size_hint: (0.3, 0.4)
        dirselect: True

For example, in python language

FileChooserListView(size_hint_x=0.3, size_hint_y=0.4, dirselect=True)

Hope it helps somebody

Ivan
  • 759
  • 10
  • 18