我正在尝试弄清楚如何从使用 asyncio 创建的 Kivy 应用程序执行 open_settings()。
不出所料,这不起作用:
Button:
text:"Open Settings"
on_release: app.open_settings()
我得到错误:
Exception:
'NoneType' object has no attribute 'bind'
应用程序用代码实例化:
if __name__ == '__main__':
def root_func():
"""
This will run both methods asynchronously and then block until they're finished
"""
root = Builder.load_string(kv) # root widget
websocket_receiver_task = asyncio.ensure_future(websocket_receiver(root))
process_queued_log_task = asyncio.ensure_future(queue_processor(root))
return asyncio.gather(ExampleViewer.kivy_runner(root, websocket_receiver_task, process_queued_log_task), websocket_receiver_task, process_queued_log_task)
loop = asyncio.get_event_loop()
loop.run_until_complete(root_func())
loop.close()
ExampleViewer 代码为:
class ExampleViewer(BoxLayout):
"""
This class, which runs Kivy, is run by the asyncio loop as one of the coroutines.
"""
def __init__(self, **kwargs):
super(ExampleViewer, self).__init__(**kwargs)
async def kivy_runner(self, websocket_receiver_task, process_queued_log_task):
try:
print("Starting kivy.")
await async_runTouchApp(self) # run Kivy
print('App terminated.')
# cancel all the other tasks that may be running
websocket_receiver_task.cancel()
process_queued_log_task.cancel()
except Exception as exc:
print(f"Exception:\n{exc}")
关于如何启动设置的建议将不胜感激。 :)
我曾尝试在谷歌上搜索建议,但一无所获。 :(
因为你没有一个
App
实例,我不认为你可以调用open_settings()
方法。但是,如果您查看 Kivy 示例中的 asyncio_advanced.py
代码,它会显示如何使用实际的 App
实例运行异步应用程序。也许你可以修改你的代码以那样工作。