如何使用 kivymd 创建启动画面
How to create a Splash Screen using kivymd
我是 kivymd 的新手,我正在尝试使用 kivymd 创建启动画面。但是所有教程都显示单击按钮后的屏幕转换。我想要的是 .kv 文件中的模块化代码,以便 WindowManager 首先打开启动画面,等待 5 秒,然后打开应用程序的主屏幕。
我尝试了以下代码,但我无法理解如何使用时钟 class 先打开启动画面,然后等待 5 秒,最后转到主屏幕?
PS - 我在 Whosebug 中看到了这个问题,但这并没有解决我的目的
Main.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock
import time
main_kv = Builder.load_file(r'.\kv\main.kv')
Window.size = (400, 700)
class SplashScreen():
"""This class will show the splash screen of Docto365"""
class HomeScreen():
"""This class will show the Home screen of Doctor365"""
class WindowManager(ScreenManager):
"""This class will handle the screen transitions"""
class Test(MDApp):
def build(self):
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
return main_kv
if __name__ == '__main__':
Test().run()
main.kv
<WindowManager>:
SplashScreen:
HomeScreen
<SplashScreen>:
MDLabel:
text: "Splash Screen"
<HomeScreen>:
MDLabel:
text: "Home Screen"
你可以使用Screen
的on_enter()
方法,像这样:
class SplashScreen(Screen):
"""This class will show the splash screen of Docto365"""
def on_enter(self, *args):
Clock.schedule_once(self.switch_to_home, 5)
def switch_to_home(self, dt):
self.manager.current = 'Home'
以及对您的 .kv
:
的一些必要更改
WindowManager: # eliminate "<>" to make this a root widget
SplashScreen:
HomeScreen
<SplashScreen>:
name: 'Splash' # name is required
MDLabel:
text: "Splash Screen"
<HomeScreen>:
name: 'Home' # name is required
MDLabel:
text: "Home Screen"
并将您的调用移至 build()
方法内的 builder.load_file()
:
class Test(MDApp):
def build(self):
main_kv = Builder.load_file(r'.\kv\main.kv')
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
return main_kv
您当前的代码存在一些缺陷:
- 由于您要加载 kivy 代码并将其返回到应用程序的
build
方法中,因此您需要在 kivy 代码中有一个根小部件。
- 您的自定义屏幕 classes 必须继承自 Kivy 的
Screen
class (kivy.uix.screenmanager.Screen
).
- 确保在所有 class 声明之后加载你的 kivy 代码,因为你的 kivy 代码需要那些 classes,因此如果所需的 class 不是,它们会引发错误在您的 python 代码中找到。
现在回答你的问题,因为你想在固定的时间后自动更改屏幕,你可以使用 Clock.schedule_once
。
更新代码:
Kivy
- 将 WindowManager
设置为 root 并为屏幕命名
WindowManager:
SplashScreen:
name: "splash"
HomeScreen:
name: "home"
<SplashScreen>:
MDLabel:
text: "Splash Screen"
<HomeScreen>:
MDLabel:
text: "Home Screen"
python
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock
import time
#main_kv = Builder.load_string(kv)
Window.size = (400, 700)
class SplashScreen(Screen):
"""This class will show the splash screen of Docto365"""
class HomeScreen(Screen):
"""This class will show the Home screen of Doctor365"""
class WindowManager(ScreenManager):
"""This class will handle the screen transitions"""
class Test(MDApp):
def build(self):
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
self.window_manager = Builder.load_string(kv)
Clock.schedule_once(self.go_to_home, 5)
return self.window_manager
def go_to_home(self, *args):
self.window_manager.current = 'home'
if __name__ == '__main__':
Test().run()
注意:您可以使用不同的 screen transition 使其看起来更像是从飞溅到主页的渐变
PS:我建议您不要将此功能用于 kivy 移动应用程序,因为除了默认启动外,这只会为您的用户增加额外的等待时间时间。
我是 kivymd 的新手,我正在尝试使用 kivymd 创建启动画面。但是所有教程都显示单击按钮后的屏幕转换。我想要的是 .kv 文件中的模块化代码,以便 WindowManager 首先打开启动画面,等待 5 秒,然后打开应用程序的主屏幕。
我尝试了以下代码,但我无法理解如何使用时钟 class 先打开启动画面,然后等待 5 秒,最后转到主屏幕?
PS - 我在 Whosebug 中看到了这个问题,但这并没有解决我的目的
Main.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock
import time
main_kv = Builder.load_file(r'.\kv\main.kv')
Window.size = (400, 700)
class SplashScreen():
"""This class will show the splash screen of Docto365"""
class HomeScreen():
"""This class will show the Home screen of Doctor365"""
class WindowManager(ScreenManager):
"""This class will handle the screen transitions"""
class Test(MDApp):
def build(self):
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
return main_kv
if __name__ == '__main__':
Test().run()
main.kv
<WindowManager>:
SplashScreen:
HomeScreen
<SplashScreen>:
MDLabel:
text: "Splash Screen"
<HomeScreen>:
MDLabel:
text: "Home Screen"
你可以使用Screen
的on_enter()
方法,像这样:
class SplashScreen(Screen):
"""This class will show the splash screen of Docto365"""
def on_enter(self, *args):
Clock.schedule_once(self.switch_to_home, 5)
def switch_to_home(self, dt):
self.manager.current = 'Home'
以及对您的 .kv
:
WindowManager: # eliminate "<>" to make this a root widget
SplashScreen:
HomeScreen
<SplashScreen>:
name: 'Splash' # name is required
MDLabel:
text: "Splash Screen"
<HomeScreen>:
name: 'Home' # name is required
MDLabel:
text: "Home Screen"
并将您的调用移至 build()
方法内的 builder.load_file()
:
class Test(MDApp):
def build(self):
main_kv = Builder.load_file(r'.\kv\main.kv')
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
return main_kv
您当前的代码存在一些缺陷:
- 由于您要加载 kivy 代码并将其返回到应用程序的
build
方法中,因此您需要在 kivy 代码中有一个根小部件。 - 您的自定义屏幕 classes 必须继承自 Kivy 的
Screen
class (kivy.uix.screenmanager.Screen
). - 确保在所有 class 声明之后加载你的 kivy 代码,因为你的 kivy 代码需要那些 classes,因此如果所需的 class 不是,它们会引发错误在您的 python 代码中找到。
现在回答你的问题,因为你想在固定的时间后自动更改屏幕,你可以使用 Clock.schedule_once
。
更新代码:
Kivy
- 将 WindowManager
设置为 root 并为屏幕命名
WindowManager:
SplashScreen:
name: "splash"
HomeScreen:
name: "home"
<SplashScreen>:
MDLabel:
text: "Splash Screen"
<HomeScreen>:
MDLabel:
text: "Home Screen"
python
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock
import time
#main_kv = Builder.load_string(kv)
Window.size = (400, 700)
class SplashScreen(Screen):
"""This class will show the splash screen of Docto365"""
class HomeScreen(Screen):
"""This class will show the Home screen of Doctor365"""
class WindowManager(ScreenManager):
"""This class will handle the screen transitions"""
class Test(MDApp):
def build(self):
self.theme_cls.primary_palette = 'Indigo'
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.theme_style = 'Light'
self.window_manager = Builder.load_string(kv)
Clock.schedule_once(self.go_to_home, 5)
return self.window_manager
def go_to_home(self, *args):
self.window_manager.current = 'home'
if __name__ == '__main__':
Test().run()
注意:您可以使用不同的 screen transition 使其看起来更像是从飞溅到主页的渐变
PS:我建议您不要将此功能用于 kivy 移动应用程序,因为除了默认启动外,这只会为您的用户增加额外的等待时间时间。