Python 如何在kivymd中的函数开始时显示加载屏幕?
在从web获取数据期间,我想在Kivymd应用程序中使用加载屏幕。但当我运行代码时,获取数据后会出现加载屏幕 我想显示加载屏幕,从web获取一些数据,然后在新屏幕上显示结果。Python 如何在kivymd中的函数开始时显示加载屏幕?,python,kivy,python-3.8,kivymd,Python,Kivy,Python 3.8,Kivymd,在从web获取数据期间,我想在Kivymd应用程序中使用加载屏幕。但当我运行代码时,获取数据后会出现加载屏幕 我想显示加载屏幕,从web获取一些数据,然后在新屏幕上显示结果。 这是我的get_data功能的一部分。此功能在用户单击按钮时运行 def get_data(self): self.root.ids.MainScreen.pos_hint = {"center_x": .5, "center_y": 50} # Hide main scre
这是我的
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
装载几乎需要10秒钟。我将屏幕移动代码放在函数的顶部,但为什么屏幕移动代码在函数之后运行?如何解决这个问题
我正在使用Windows 10和Python 3.8。您可以使用
线程
或时钟。计划
在完成所有请求工作之前移动到加载屏幕。查看更多详细信息
更新:
以下是带参数的线程代码:
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
您可以使用
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
threading.Thread(target = function_to_get_data, args=(param,))
def function_to_get_data(self, param):
#code to get data
你可以使用窗口管理器。如果没有完整的代码,很难说,但类似于:
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_数据在MainScreen类中,LoadingScreen和MainScreen定义为窗口管理器中的屏幕,例如(单位:千伏)
WindowManager:
加载屏幕:
主屏幕:
:
id:主窗口
...
:
id:加载窗口
...
您可以使用窗口管理器。如果没有完整的代码,很难说,但类似于:
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_数据在MainScreen类中,LoadingScreen和MainScreen定义为窗口管理器中的屏幕,例如(单位:千伏)
WindowManager:
加载屏幕:
主屏幕:
:
id:主窗口
...
:
id:加载窗口
...
这似乎有效,但如果我想将参数传递给函数以获取数据,该怎么办?我想我不能在时钟中使用参数。schedule\u once(函数\u-to\u-get\u-data)
是调用部分,是get\u-data部分。(我不知道如何在堆栈溢出注释中插入多行代码。)但当我运行此代码时,它会给我错误,TypeError:get_data()接受1个位置参数,但给出了2个
。如何解决这个问题?如果您想要传递一些数据,那么您可以做的是创建一个类变量,然后在时钟函数中访问该类变量,或者您可以使用线程。线程也可以做同样的事情,也可以接受变量。关于错误,Clock.schedule\u还提供了它作为参数执行的时间。我忘了,抱歉,我已经更新了代码,它工作得很好。谢谢这似乎是可行的,但如果我想将参数传递给函数以获取数据,该怎么办?我想我不能在时钟中使用参数。schedule\u once(函数\u-to\u-get\u-data)
是调用部分,是get\u-data部分。(我不知道如何在堆栈溢出注释中插入多行代码。)但当我运行此代码时,它会给我错误,TypeError:get_data()接受1个位置参数,但给出了2个
。如何解决这个问题?如果您想要传递一些数据,那么您可以做的是创建一个类变量,然后在时钟函数中访问该类变量,或者您可以使用线程。线程也可以做同样的事情,也可以接受变量。关于错误,Clock.schedule\u还提供了它作为参数执行的时间。我忘了,抱歉,我已经更新了代码,它工作得很好。谢谢