python - 如何在 kivymd 功能开始时显示加载屏幕?
问题描述
在从 Web 获取数据期间,我想在我的 Kivymd 应用程序中使用加载屏幕。但是当我运行我的代码时,获取数据后会出现加载屏幕。
我想显示加载屏幕,从网络获取一些数据,然后在新屏幕上显示结果。
这是我get_data
职能的一部分。此功能在用户单击按钮时运行。
def get_data(self):
self.root.ids.MainScreen.pos_hint = {"center_x": .5, "center_y": 50} # Hide main screen
self.root.ids.LoadingScreen.pos_hint = {"center_x": .5, "center_y": .5} # Show loading screen
requests.get("https//.....")
# Code more
加载大约需要十秒钟。我将屏幕移动代码放在我的函数顶部,但为什么屏幕移动代码在函数之后运行?如何解决这个问题?
我正在使用 Windows 10 和 Python 3.8。
解决方案
在所有请求工作完成之前,您可以使用threading
或Clock.schedule
移动到加载屏幕。在此处查看更多详细信息
def get_data(self):
self.root.ids.MainScreen.pos_hint = {"center_x": .5, "center_y": 50} # Hide main screen
self.root.ids.LoadingScreen.pos_hint = {"center_x": .5, "center_y": .5} # Show load screen
Clock.schedule_once(function_to_get_data)
def function_to_get_data(self, *args):
#code to get data
更新: 这是带参数的线程代码:
def get_data(self):
self.root.ids.MainScreen.pos_hint = {"center_x": .5, "center_y": 50} # Hide main screen
self.root.ids.LoadingScreen.pos_hint = {"center_x": .5, "center_y": .5} # Show load screen
threading.Thread(target = function_to_get_data, args=(param,))
def function_to_get_data(self, param):
#code to get data
推荐阅读
- ios - 应用平移手势后如何在捏合手势上定位视图?
- python - 如何在pymunk和pyglet中让相机跟随汽车
- python - Python Django Celery,终止正在运行的任务不起作用
- php - 如何在 wordpress 帖子中列出存储在谷歌云存储桶中的文件?
- python-3.x - 如何在 lambda 中为 python3 赋值?
- flutter - Flutter Sembast 插入或更新
- php - Laravel 作业队列调度现在不会像往常一样失败
- javascript - 如何检查 x 和 y 是否在对象中?
- python - python中的输出与python3不同(相同的脚本)
- c++ - 在其特征向量上投影点云