简体   繁体   中英

Python kivy change screen automatically after few seconds

I'm making an app in python kivy, and I'm having a little problem. I'm trying to change from my "StartScreen" to my "TacScreen" automatically after 3 seconds. How can I do that? I tried to solve this problem but couldn't find a solution. Below is my code any help is appreciated, Thank You!

main.py

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.image import Image
from kivy.core.audio import SoundLoader
from kivy.clock import Clock


class StartScreen(Screen):
    pass


class TacScreen(Screen):
    pass


class MainApp(App):
    pass



    def change_screen(self, screen_name):
        self.root.current = screen_name


MainApp().run()

startscreen.kv

#:import utils kivy.utils
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<StartScreen>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "Soccer Lineup.png"

    FloatLayout:
        Button:
            font_size: dp(20)
            font_name: 'SackersGothicStd-Medium.otf'
            text: "Start"
            color: "gold"
            pos_hint: { "center_x": .5, "center_y": .3}
            size: 80, 55
            size_hint: None, None
            background_normal: ''
            background_color: (57/255.0, 179/255.0, 242/255.0, .10)



            on_release:
                root.manager.transition = FadeTransition()
                app.change_screen("tac_screen")

tacscreen.kv

#:import utils kivy.utils


<TacScreen>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "Field.png"

main.kv

#:include startscreen.kv
#:include tacscreen.kv


ScreenManager:
    id: screen_manager
    StartScreen:
        name: "start_screen"
        id: start_screen
    TacScreen:
        name: "tac_screen"
        id: tac_screen

You can use the on_start() method of an App along with Clock.schedule_once() to do that:

class MainApp(App):
    def on_start(self):
        Clock.schedule_once(partial(self.change_screen, "tac_screen"), 3)

    def change_screen(self, screen_name, *args):
        self.root.current = screen_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM