<i id='9DR6G'><tr id='9DR6G'><dt id='9DR6G'><q id='9DR6G'><span id='9DR6G'><b id='9DR6G'><form id='9DR6G'><ins id='9DR6G'></ins><ul id='9DR6G'></ul><sub id='9DR6G'></sub></form><legend id='9DR6G'></legend><bdo id='9DR6G'><pre id='9DR6G'><center id='9DR6G'></center></pre></bdo></b><th id='9DR6G'></th></span></q></dt></tr></i><div id='9DR6G'><tfoot id='9DR6G'></tfoot><dl id='9DR6G'><fieldset id='9DR6G'></fieldset></dl></div>
<tfoot id='9DR6G'></tfoot>
    1. <legend id='9DR6G'><style id='9DR6G'><dir id='9DR6G'><q id='9DR6G'></q></dir></style></legend>

      <small id='9DR6G'></small><noframes id='9DR6G'>

        • <bdo id='9DR6G'></bdo><ul id='9DR6G'></ul>
      1. 如何获取在 Kivy 中使用 fileChooser 选择的文件的信

        时间:2023-10-10
        <i id='E5L9y'><tr id='E5L9y'><dt id='E5L9y'><q id='E5L9y'><span id='E5L9y'><b id='E5L9y'><form id='E5L9y'><ins id='E5L9y'></ins><ul id='E5L9y'></ul><sub id='E5L9y'></sub></form><legend id='E5L9y'></legend><bdo id='E5L9y'><pre id='E5L9y'><center id='E5L9y'></center></pre></bdo></b><th id='E5L9y'></th></span></q></dt></tr></i><div id='E5L9y'><tfoot id='E5L9y'></tfoot><dl id='E5L9y'><fieldset id='E5L9y'></fieldset></dl></div>
      2. <tfoot id='E5L9y'></tfoot>

          <bdo id='E5L9y'></bdo><ul id='E5L9y'></ul>
        • <legend id='E5L9y'><style id='E5L9y'><dir id='E5L9y'><q id='E5L9y'></q></dir></style></legend>

                1. <small id='E5L9y'></small><noframes id='E5L9y'>

                    <tbody id='E5L9y'></tbody>
                  本文介绍了如何获取在 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 选择的文件的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在简单的 Kivy 应用程序中将 Pandas 数据框显示 下一篇:暂停 python 脚本,直到事件发生而不挂起/阻塞

                  相关文章

                  最新文章

                    <bdo id='WEWdx'></bdo><ul id='WEWdx'></ul>

                    <i id='WEWdx'><tr id='WEWdx'><dt id='WEWdx'><q id='WEWdx'><span id='WEWdx'><b id='WEWdx'><form id='WEWdx'><ins id='WEWdx'></ins><ul id='WEWdx'></ul><sub id='WEWdx'></sub></form><legend id='WEWdx'></legend><bdo id='WEWdx'><pre id='WEWdx'><center id='WEWdx'></center></pre></bdo></b><th id='WEWdx'></th></span></q></dt></tr></i><div id='WEWdx'><tfoot id='WEWdx'></tfoot><dl id='WEWdx'><fieldset id='WEWdx'></fieldset></dl></div>

                      <small id='WEWdx'></small><noframes id='WEWdx'>

                    1. <tfoot id='WEWdx'></tfoot>
                      <legend id='WEWdx'><style id='WEWdx'><dir id='WEWdx'><q id='WEWdx'></q></dir></style></legend>