繁体   English   中英

向 Kivy.app.on_start 函数发送参数

[英]Sending arguments to Kivy.app.on_start function

我有一些这样的代码(MRE):

from kivy.app import App

    def my_function(*arguments):
       *some actions with App.root*

MyApp = App()
MyApp.on_start = my_function(some arguments)
MyApp.run()

它返回

 AttributeError: 'NoneType' object has no attribute '*what I'm trying to access*'

玩了一会儿,我了解到问题在于我在分配时发送参数

MyApp.on_start = my_function(some arguments)

我一直在尝试多种解决方案,例如使用

setattr(MyApp, 'on_start', my_function(some arguments))

并分配给

MyApp.run(some arguments)

打电话,但没有任何效果。 我该怎么办?

谢谢!

我认为你可以使用partial来做到这一点:

from functools import partial
from kivy.app import App


def my_function(*arguments):
    print('in my_function')
    for arg in arguments:
        print('\t', arg)

MyApp = App()
MyApp.on_start = partial(my_function, 'abba', 'dabba')
MyApp.run()

只设置App类的on_start函数来调用你的my_function类怎么样?

from kivy.app import App

class MyApp(App):
    def on_start(self):
        self.my_function('foo','bar')

    def my_function(self, *args):
        print(args)

MyApp().run()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM