如何在 kivymd 的功能开始时显示加载屏幕?
How to show loading screen at start of function in 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
移动到加载屏幕。查看更多详情here
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
您可以使用 window 经理。没有完整的代码很难说,但类似于:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
class MainScreen(Screen):
...
def get_data(self):
self.parent.current = 'LoadingWindow'
get your data
wait for it to return
self.parent.current = 'MainWindow'
...
class LoadingScreen(Screen):
pass
...
class WindowManager(ScreenManager):
pass
这假设 a.o。 get_data 在 MainScreen Class 中,LoadingScreen 和 MainScreen 在 window 管理器中被定义为屏幕,就像这样(在 .kv 中)
WindowManager:
LoadingScreen:
MainScreen:
<MainScreen>:
id: mainWindow
...
<LoadingScreen>:
id: LoadingWindow
...
我想在从 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
移动到加载屏幕。查看更多详情here
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
您可以使用 window 经理。没有完整的代码很难说,但类似于:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
class MainScreen(Screen):
...
def get_data(self):
self.parent.current = 'LoadingWindow'
get your data
wait for it to return
self.parent.current = 'MainWindow'
...
class LoadingScreen(Screen):
pass
...
class WindowManager(ScreenManager):
pass
这假设 a.o。 get_data 在 MainScreen Class 中,LoadingScreen 和 MainScreen 在 window 管理器中被定义为屏幕,就像这样(在 .kv 中)
WindowManager:
LoadingScreen:
MainScreen:
<MainScreen>:
id: mainWindow
...
<LoadingScreen>:
id: LoadingWindow
...