Python 将参数发送到Kivy.app.on_启动函数
我有一些代码是这样的(MRE): 它回来了Python 将参数发送到Kivy.app.on_启动函数,python,kivy,Python,Kivy,我有一些代码是这样的(MRE): 它回来了 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))
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)
打电话,但没用。
我该怎么办
谢谢大家! 我认为您可以使用
部分来完成此操作:
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()
将应用程序
类的启动
功能设置为调用您的我的
类如何
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()
张贴一个完整的例子。你为什么还要尝试设置一些外部函数的启动?除了有多个函数,在启动时调用哪个函数取决于运行应用程序之前的后端。我知道它不在MRE中,但我认为它不应该在那里,因为它与问题没有直接关系。难道你不能在启动时的函数中有一些逻辑来告诉以后调用哪个函数吗?
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()