如何在Kivy中获取使用fileChooser选择的文件的信息? [英] How do I get info of a file selected with fileChooser in Kivy?

查看:742
本文介绍了如何在Kivy中获取使用fileChooser选择的文件的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取通过fileChooser选择的文件的信息?这是我拥有的一些代码块:

How do I grab info of a file I select through the fileChooser? Here are some chunks of code I have:

self.fileChooser = fileChooser = FileChooserListView(size_hint_y=None, path='/home/')
...
btn = Button(text='Ok')
btn.bind(on_release=self.load(fileChooser.path, fileChooser.selection))
...
def load(self, path, selection):
    print path,  selection

这是在我最初打开fileChooser时在实例中打印路径和选择的内容.当我选择一个文件并单击确定"时,什么也没发生.

What this does is print the path and the selection in the instance when I initially open the fileChooser. When I select a file and click 'Ok', nothing happens.

推荐答案

btn.bind(on_release=self.load(fileChooser.path, fileChooser.selection))

...
def load(self, path, selection):
    print path,  selection

这是python语法的误用.问题是,您需要将 function 传递给btn.bind.存储该函数,然后在发生on_release事件时,将调用该函数.

This is a misuse of python syntax. The problem is, you need to pass a function to btn.bind. The function is stored, then when the on_release event occurs, the function is called.

您所做的不是传递函数,而是简单地调用它并传递结果.这就是为什么打开文件选择器时看到的路径和选择仅打印一次的原因-这是实际调用该函数的唯一且唯一的一次.

What you have done is not pass in the function, but simply call it and pass the result. That's why you see the path and selection printed once when you open the filechooser - that's the one and only time the function is actually called.

相反,您需要传递要调用的实际函数.由于变量作用域的限制,您在这里必须格外小心,并且有多种潜在的解决方案.以下是一种可能性的基础:

Instead, you need to pass in the actual function you want to call. You have to be a bit careful here because of variable scoping, and there are multiple potential solutions. Below is the basics of one possibility:

def load_from_filechooser(self, filechooser):
    self.load(filechooser.path, filechooser.selection)
def load(self, path, selection):
    print path,  selection
...
from functools import partial
btn.bind(on_release=partial(self.load_from_filechooser, fileChooser))

partial函数接受一个函数和一些参数,并返回一个自动传递这些参数的新函数.这意味着,在on_release发生时,bind实际上有一些要调用的东西,它依次调用load_from_filechooser,后者又调用您的原始加载函数.

The partial function takes a function and some arguments, and returns a new function that automatically passes those arguments. That means bind actually has something to call when on_release occurs, which in turn calls load_from_filechooser which in turn calls your original load function.

您也可以不做部分操作,但这是一种有用的通用技术,在这种情况下(我认为)有助于弄清楚发生了什么.

You could also do this without partial, but it's a useful general technique and in this case helps (I think) to make it clear what's going on.

我使用了对fileChooser的引用,因为您不能直接在函数中引用fileChooser.path和fileChooser.selection-您只能在定义函数时获取它们的值.这样,我们将跟踪fileChooser本身,并仅在以后调用该函数时提取路径和选择.

I used a reference to fileChooser because you can't reference fileChooser.path and fileChooser.selection directly in your function - you would only get their values at the time the function is defined. This way, we track the fileChooser itself and only extract the path and selection when the function is later called.

这篇关于如何在Kivy中获取使用fileChooser选择的文件的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆