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

查看:51
本文介绍了如何获取在 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,而后者又会调用你原来的 load 函数.

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天全站免登陆