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

    <tfoot id='F9vy3'></tfoot>

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

    1. <legend id='F9vy3'><style id='F9vy3'><dir id='F9vy3'><q id='F9vy3'></q></dir></style></legend>
      1. <small id='F9vy3'></small><noframes id='F9vy3'>

        kivy python通过按钮单击将参数传递给函数

        时间:2023-10-10
            <tbody id='9OTE9'></tbody>

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

                  <tfoot id='9OTE9'></tfoot>

                  <legend id='9OTE9'><style id='9OTE9'><dir id='9OTE9'><q id='9OTE9'></q></dir></style></legend>

                  本文介绍了kivy python通过按钮单击将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  按下按钮调用函数时,我无法将参数传递给函数.用 kivy 语言可以这样做:

                  I am having trouble passing parameters to function when calling it with button press. One could do it like this in kivy language:

                  Button: 
                     on_press: root.my_function('btn1')
                  

                  但我想用 python 来做,因为我想用循环创建更多的按钮.目前我在 python 中这样调用我的函数:

                  but I would like to do it in python, as I would like to create a larger number of buttons with a loop. Currently I call my function in python like this:

                  Button(on_press=self.my_function)
                  

                  但正如我所说,如果我尝试像这样将参数传递给函数,我会得到一个AssertionError: None is not callable",如下所示:

                  but as I said, if I try to pass a parameter to the function like this, I get an 'AssertionError: None is not callable', like this:

                  Button(on_press=self.my_function('btn1'))
                  

                  推荐答案

                  Button(on_press=self.my_function)
                  

                  这是传递函数作为参数.

                  Button(on_press=self.my_function('btn1'))
                  

                  这是调用函数并将返回值作为参数传递给on_press.由于返回值为 None,因此您会收到错误消息.

                  This is calling the function and passing the returned value as the argument to on_press. Since the returned value is None, you get your error.

                  您需要传递一个调用普通函数并自动传递参数的新函数.总的来说,使用 functools.partial 比较方便:

                  You instead need to pass a new function that calls your normal function and automatically passes the argument. In general, it's convenient to use functools.partial:

                  from functools import partial
                  Button(on_press=partial(self.my_function, 'btn1'))
                  

                  您还可以使用 lambda 函数:

                  You can also use a lambda function:

                  Button(on_press=lambda *args: self.my_function('btn1', *args))
                  

                  这篇关于kivy python通过按钮单击将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Kivy:拖放,获取文件路径 下一篇:使用自定义小部件 kivy

                  相关文章

                  最新文章

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

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

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