問題描述
如何獲取通過 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 選擇的文件的信息?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!