简体   繁体   中英

Index error when choosing file in a kivy popup window

def popping(self, button_instance):
        self.small_page = Popup(title='Choose jpg or png file',size_hint=(.8,.8))
        self.scroll = ScrollView()
        self.small_page.add_widget(self.scroll)
        file_choose = FileChooserListView()
        self.scroll.add_widget(file_choose)
        self.upload_pic = Button(text='Upload', size_hint=(1,.2), on_press= self.uploading(file_choose.selection))
        self.small_page.add_widget(self.upload_pic)
          
        
        
        self.small_page.open()
        
def uploading(self, filename):
        profile_pic.source = filename[0]

i have a kivy popup window and that goes to a file chooser, everytime i try to access a file it gives the error, if possible could answer be written in python language rather than kivy.

IndexError: list index out of range
            

The problem is with the line:

self.upload_pic = Button(text='Upload', size_hint=(1,.2), on_press= self.uploading(file_choose.selection))

That line executes the self.uploading(file_choose.selection) when the Button is defined, well before you have a chance to select anything in the FileChooser . You can use partial to define a function to be called like this:

self.upload_pic = Button(text='Upload', size_hint=(1, .2), on_press=partial(self.uploading, file_choose))

The partial defines a function (and its args), but does not call it. Then your self.uploading() method can be something like:

def uploading(self, file_chooser, button):
    print(file_chooser.selection[0])

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