0

I'm very new to Kivy and through a lot of trial and error have gotten a pretty decent working app.

What I can't for the life of me figure out is how to get the FileChooser to update its path based on what I've selected (directory or otherwise).

.selection always returns and empty list and .path always returns the starting directory even after I have moved around. I'm missing some event somewhere and have spent a lot of time trying to figure this out and was hoping someone here could help.

I thought .selection and .path would update as I navigated but they seem stuck on their first/default values. I made the testbutton just to see if I could get path or selection to print out/update.

This all gets triggered once they hit another button

def testbutton(self,test,iconview,*args):
    print(test,iconview.path)

filepop=Popup(title='SaveFile')
FileChooserLayout=BoxLayout(orientation='vertical')
ButtonArea=BoxLayout(orientation='horizontal',spacing=50,size_hint=(.5,.5),pos_hint={'center_x': 0.5, 'center_y': 0.5})    
listview=FileChooserListView(path='somepath',dirselect=True)
test=listview.path
testbutton=Button(text='test',on_press=partial(self.testbutton, test,iconview))
ButtonArea.add_widget(testbutton)
FileChooserLayout.add_widget(ButtonArea)
filepop.add_widget(FileChooserLayout)

Thanks for any help!

LucyMLi
  • 657
  • 4
  • 14

2 Answers2

0

In FileChooserListView, use on_selection event and pass *args to the method. Please refer to the example for details.

snippets

        FileChooserListView:
            on_selection: 
                app.root.selected_file(*args)

Example

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.lang import Builder

Builder.load_string('''
#:kivy 1.10.0

<SaveFile>:
    title: 'Save File'

    # FileChooserLayout
    BoxLayout:
        orientation: 'vertical'

        # ButtonArea
        BoxLayout:
            orientation: 'horizontal'
            spacing: 50
            size_hint: (.5,.5)
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}

            FileChooserListView:
                on_selection: 
                    app.root.selected_file(*args)

<RootWidget>:

''')


class SaveFile(Popup):
    pass


class RootWidget(BoxLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        filepop = SaveFile()
        filepop.open()

    def selected_file(self, *args):
        print("*args=", args)
        for arg in args:
            print("arg=", arg)


class DemoApp(App):
    def build(self):
        return RootWidget()


if __name__ == '__main__':
    DemoApp().run()

Output

Img01 - *args displayed

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Thank you for your response. I was trying to do this w/o using .kv files. I hit another wall earlier on making buttons iteratively and found it easier (for me) to do it in straight python. I've done a pretty piss poor job creating separate classes for different widgets. Currently almost the entire app is under 1 class... So when I try to use your method which would be something like self.on_selection it just refers to the class which makes sense. I can't make it refer to itself because that's the same line I'm calling the on_selection. – James Costello Jun 08 '18 at 18:08
0

I found success with the event on_submit

https://kivy.org/docs/_modules/kivy/uix/filechooser.html

I had to make each type of FileChooser in my program it's own class and give it the on_submit function then I can get a response of the address to print.

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

This post really helped: Kivy FileChooser doubleclick

  • i'm new to kivy too, but you can 'bind' to the on_submit event, or pass `on_submit = funcname` to the constructor, and only use 1 class – fuzzyTew Nov 16 '20 at 14:23