我想在后台执行某些操作时显示等待屏幕。实际上,尽管root.transition.current正常工作,该屏幕也不显示等待屏幕。
import sys
import time
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
kv = '''
<MyScreenManager>:
AcceptShutdown:
Waiting:
<AcceptShutdown>
name: 'acceptShutdown'
BoxLayout:
orientation: 'vertical'
BoxLayout:
Label:
text: 'Do you really want to exit?'
Button:
text: 'OK'
on_press:
root.manager.transition.direction = 'down' #these three
root.manager.current = 'waiting' #lines are
Clock.schedule_once(root.shutdown, 5) #the problem
<Waiting>
name: 'waiting'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Take a break.'
'''
Builder.load_string(kv)
class Waiting(Screen):
pass
class MyScreenManager(ScreenManager):
pass
class AcceptShutdown(Screen):
def shutdown(self, time):
sys.exit()
pass
class Shutdown(Screen):
pass
class Panel(App):
sm = MyScreenManager()
def build(self):
return self.sm
if __name__ == "__main__":
Panel().run()
它将调用该函数,但不会更改屏幕。 当我仅执行过渡时(此后不进行函数调用),它具有预期的结果。屏幕显示等待屏幕。 反之亦然(仅在没有过渡的情况下调用函数)也可以这样做。
希望有人能帮助我。
答案 0 :(得分:0)
问题在于time.sleep(5)
完全按照它说的做:Python进程休眠5秒钟,而不是更新gui以反映您刚刚所做的更改。
相反,使用Clock.schedule_once(root.shutdown, 5)
并让shutdown
期望它可以忽略的参数(Clock.schedule_once将自调度以来的时间作为自动参数传递)。这将插入root.shutdown作为Kivy稍后执行的操作,而不会阻塞绘图循环。