在Kivy for Python中,我想知道如何安排屏幕在指定的秒数后自动转换到新屏幕。我使用Kivy Clock工具安排了一个事件,但是当我运行应用程序时,它对我的代码没有响应。这是我的main.py代码:
class Opening_Screen():
def __init__(self, **kwargs):
super(Opening_Screen, self).__init__(**kwargs)
Clock.schedule_once(self.change_screen, 5)
def change_screen(self, *kwargs):
root.current = 'Options_Screen'
class Patient_Care(App):
pass
if __name__ == '__main__':
Patient_Care().run()
这是我对应的.kv文件代码:
#:import C kivy.utils.get_color_from_hex
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
ScreenManager:
transition: RiseInTransition()
Screen:
name: 'Opening_Screen'
Label:
text:'Welcome'
font_size: 40
color: 0, 0, 1, 1
pos:(self.x / 2, 250)
Button:
size_hint: (1, None)
height: 90
font_size: 30
background_normal: 'button_normal.png'
background_down: 'button_down.png'
border: (2, 2, 2, 2)
text: 'Continue'
on_press: root.current = 'Options_Screen'
Screen:
name: 'Options_Screen'
GridLayout:
padding: 15
rows: 5
spacing: 10
Button:
text:'Option 1'
Button:
text:'Option 2'
Button:
text: 'Option 3'
Button:
text: 'Option 4'
Button:
text: 'Option 5'
在我的.kv文件中,我有另一个名为" Options_Screen"的屏幕。我想从" Opening_Screen"切换5秒后到选项屏幕。当我为Opening_Screen创建一个按钮并将其设计为切换到Options_Screen(on_press:root.current =' Options_Screen')时,转换有效。但是,我的目标是使用Clock.schedule_once函数动态切换,无需按下按钮即可自动切换。
答案 0 :(得分:1)
由于您没有显示重现错误所需的代码,例如应用程序必须以App结尾,并且.kv的名称必须是小写的类的剩余部分。另一个可能的错误是Opening_Screen不会继承任何类型,所以它可能永远不会被调用等等。
所以我只想提供一个例子:
<强> patientcare.kv 强>
ScreenManager
Opening_Screen:
name: 'Opening_Screen'
Options_Screen:
name: 'Options_Screen'
<Opening_Screen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Opening_Screen'
<Options_Screen>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Options_Screen'
<强> main.py 强>
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
class Opening_Screen(Screen):
def __init__(self, **kwargs):
super(Opening_Screen, self).__init__(**kwargs)
Clock.schedule_once(self.change_screen, 5)
def change_screen(self, *kwargs):
self.manager.current = 'Options_Screen'
class Options_Screen(Screen):
pass
class PatientCareApp(App):
pass
if __name__ == '__main__':
PatientCareApp().run()
当您添加更多代码时,您只需映射新类:
<强> *。PY 强>
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.clock import Clock
class Opening_Screen(Screen):
def __init__(self, **kwargs):
super(Opening_Screen, self).__init__(**kwargs)
Clock.schedule_once(self.change_screen, 5)
def change_screen(self, *kwargs):
self.manager.current = 'Options_Screen'
class Patient_Care(App):
pass
if __name__ == '__main__':
Patient_Care().run()
<强> *。KV 强>
#:import C kivy.utils.get_color_from_hex
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
<Opening_Screen>:
ScreenManager:
transition: RiseInTransition()
Opening_Screen:
name: 'Opening_Screen'
[...]
如果您不想在屏幕中实现它,可以在App中执行。
from kivy.app import App
from kivy.clock import Clock
class Patient_Care(App):
def on_start(self):
Clock.schedule_once(self.change_screen, 5)
def change_screen(self, *kwargs):
self.root.current = 'Options_Screen'
if __name__ == '__main__':
Patient_Care().run()