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]我有一个 kivy 弹出窗口,它会转到文件选择器,每次我尝试访问文件时都会出错,如果可能的话可以用 python 语言而不是 kivy 编写答案。IndexError: list index out of range
1 回答
九州编程
TA贡献1785条经验 获得超4个赞
问题在于该行:
self.upload_pic = Button(text='Upload', size_hint=(1,.2), on_press= self.uploading(file_choose.selection))
该行在定义self.uploading(file_choose.selection)时执行Button,远在您有机会选择FileChooser. 您可以使用partial定义要调用的函数,如下所示:
self.upload_pic = Button(text='Upload', size_hint=(1, .2), on_press=partial(self.uploading, file_choose))
定义partial了一个函数(及其参数),但不调用它。那么你的self.uploading()方法可以是这样的:
def uploading(self, file_chooser, button):
print(file_chooser.selection[0])
添加回答
举报
0/150
提交
取消