[英]How to start kivy app with arguments from another kivy app
[英]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.