filechooser

1,276 views
Skip to first unread message

jcy29

unread,
Sep 29, 2011, 1:10:18 PM9/29/11
to Kivy users support
Hi,
I have a button and when i click on it it open a popup with a
filechooser .my problem is that i don't know how to chose for example
a sound file and apply it on a button using a filechooser. if someone
could help me

jcy29

unread,
Sep 29, 2011, 1:10:18 PM9/29/11
to Kivy users support

Cyril Stoller

unread,
Oct 1, 2011, 8:03:10 AM10/1/11
to Kivy users support
Hello jcy29

I also worked with the fileChooser in my TouchContinuum (https://
github.com/stocyr/TouchContinuum)
I set you up a little example with a popup containing a filChooser
object. The only problem it has is that it doesn't return a value (or
more precise it returns [] - an empty file list)... That's the same
problem I also have with MY application...

FILE: (just copy all of the following python code, paste it in a .py
file and execute it with kivy.



import kivy
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.filechooser import FileChooserListView
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView


class FilePopup(Popup):
def __init__(self, **kwargs):
super(FilePopup, self).__init__(**kwargs)

# create popup layout containing a boxLayout
content = BoxLayout(orientation='vertical', spacing=5)
self.popup = popup = Popup(title=self.title,
content=content, size_hint=(None, None), size=(600, 400))

# first, create the scrollView
self.scrollView = scrollView = ScrollView()

# then, create the fileChooser and integrate it in the
scrollView
self.fileChooser = fileChooser =
FileChooserListView(size_hint_y=None)
fileChooser.height = 500 # this is a bit ugly...
scrollView.add_widget(fileChooser)

# construct the content, widget are used as a spacer
content.add_widget(Widget(size_hint_y=None, height=5))
content.add_widget(scrollView)
content.add_widget(Widget(size_hint_y=None, height=5))

# 2 buttons are created for accept or cancel the current value
btnlayout = BoxLayout(size_hint_y=None, height=50, spacing=5)
btn = Button(text='Ok')
btn.bind(on_release=self._validate)
btnlayout.add_widget(btn)

btn = Button(text='Cancel')
btn.bind(on_release=popup.dismiss)
btnlayout.add_widget(btn)
content.add_widget(btnlayout)

# all done, open the popup !
popup.open()

def _validate(self, instance):
print self.fileChooser.selection
self.popup.dismiss()
self.popup = None
value = self.fileChooser.selection
# if the value was empty, don't change anything.
if value == '':
# do what you would do if the user didn't select any file
return

# do what you would do if the user selected a file.
print 'choosen file: %s' % value

if __name__ == '__main__':
from kivy.app import App

class FileChooserApp(App):

def build(self):
FilePopup()

FileChooserApp().run()

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 8:06:51 AM10/1/11
to kivy-...@googlegroups.com

Thx i will try it and i tell you after if found a solution

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 9:51:28 AM10/1/11
to kivy-...@googlegroups.com

The filechooser.selection doesn't work.

Cyril Stoller

unread,
Oct 1, 2011, 9:52:24 AM10/1/11
to kivy-...@googlegroups.com

Well, I assumed the same J So what to do?

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 9:53:56 AM10/1/11
to kivy-...@googlegroups.com

i have no idea of how to solve that

Cyril Stoller

unread,
Oct 1, 2011, 10:00:28 AM10/1/11
to kivy-...@googlegroups.com

The problem is in the file <kivy location>/kivy/kivy/uix/fileChooser.py

If you set multiselect=true (to say, you left it on defalut multiselect=false) the line 193 gives you an EMPTY selection list.

But now as i look at it, it’s pretty clear what the idea is : the file path and name aren’t actually stored in fileChooser.selection then, but in fileChooser.path ;-)

When you select multiselection, the fileCHooser.selection gives you the required value ;-)

 

Did you understand so far ? à for single file selection (default) choose fileChooser.path. for multiselection take the fileChooser.selection property.

Hope that helped. I’ll try it out right now.

 

Cyril Stoller

Cyril Stoller

unread,
Oct 1, 2011, 10:01:38 AM10/1/11
to kivy-...@googlegroups.com

Sorry, little fault:

 

The problem is in the file <kivy location>/kivy/kivy/uix/fileChooser.py

If you didn’t set multiselect=true (to say, you left it on defalut multiselect=false) the line 193 gives you an EMPTY selection list.

But now as i look at it, it’s pretty clear what the idea is : the file path and name aren’t actually stored in fileChooser.selection then, but in fileChooser.path ;-)

When you select multiselection, the fileCHooser.selection gives you the required value ;-)

 

Did you understand so far ? à for single file selection (default) choose fileChooser.path. for multiselection take the fileChooser.selection property.

Hope that helped. I’ll try it out right now.

 

Cyril Stoller

 

 

Von: kivy-...@googlegroups.com [mailto:kivy-...@googlegroups.com] Im Auftrag von Jean Claude Twagiramungu
Gesendet: Samstag, 1. Oktober 2011 15:54
An: kivy-...@googlegroups.com
Betreff: Re: AW: filechooser

 

i have no idea of how to solve that

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 10:22:21 AM10/1/11
to kivy-...@googlegroups.com

I don't really understand may be you can give me an example when you will try it and if it work

Cyril Stoller

unread,
Oct 1, 2011, 10:27:38 AM10/1/11
to kivy-...@googlegroups.com

Ok, sorry another time. This idea was just shit. The fileChooser.py actually works like the following:

if i select an entry (line 166) and the entry is a file (line 178) and not a directory, the event « on_submit » is fired by the fileChooser object. This is just like with a button object – you can now bind this event to a custom function :

 

if you created the following:

filechooserinstance = fileChooser()

 

you can bind it with:

filechooserinstance.bind(on_submit=self.whateverfunction)

 

and then later on in your actual class :

 

def whateverfunction(self, fileChooserInstance, selected, touch) # <selected> is a list containing only one entry : your filename.

    print selected[0] # and your done.

 

 

 

I appended you the file that works now great here.

settingfile.py

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 10:31:18 AM10/1/11
to kivy-...@googlegroups.com

Ok great a lot i will try it

Mathieu Virbel

unread,
Oct 1, 2011, 12:05:56 PM10/1/11
to kivy-...@googlegroups.com
Cyril,

When you found something unclear in the documentation, it would be
über nice if you can do the change, and send us a pull request in
github.
Or at least, report a bug report. Then we can imrpove the documentation :)
Can you do it for that one ?

Thanks,

Mathieu

2011/10/1 Cyril Stoller <cyril....@gmail.com>:

Jean Claude Twagiramungu

unread,
Oct 1, 2011, 12:19:39 PM10/1/11
to kivy-...@googlegroups.com

Ok i will do that

Reply all
Reply to author
Forward
0 new messages