python-3.xvideokivykivymd

How to play video AFTER changing screens in python kivy?


I am trying to run a video using kivy's video module as soon as the screen changes to the talkbot screen. Here is the code:

#some imports used for other parts not included in code, irrelevant
import kivy
kivy.require('2.2.1')
import pyttsx3
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.video import Video
from kivy.uix.image import Image
from kivy.uix.textinput import TextInput
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.label import MDLabel

#other code here, unimportant

class TalkBot(Screen):
    def __init__(self, **kwa):
        super(TalkBot, self).__init__(**kwa)
        bgbox = MDBoxLayout()
        bgvid = Video(source="speechvid.avi", allow_stretch=True, keep_ratio=False, size_hint_y=1)
        bgvid.state="play"
        bgbox.add_widget(bgvid)
        self.add_widget(bgbox)
                                                                                                                           
class MindMagic(MDApp):
    def build(self):
        self.theme_cls.material_style = "M3"
        self._app_name = "MindMagic"
        self.icon = "logo.png"
        sm = ScreenManager(transition = FadeTransition())
        sm.add_widget(winvid(name="winvid"))
        sm.add_widget(homeScreen(name="homeScreen"))
        sm.add_widget(TalkBot(name="talkbot"))
        return sm

if __name__ == "__main__":
    MindMagic().run()

Yes I know I did everything in python but I didn't have enough time to learn to write in .kv Please help me out Thanks

I have tried options like:

if ScreenManager.current_screen == "talkbot":
    bgvid.state="play"

but doesn't seem to work. The video just doesn't exist on the page.


Solution

  • for further refernce refer - https://kivy.org/doc/stable/api-kivy.uix.screenmanager.html#changing-transitions

        import kivy
        import pyttsx3
        from kivymd.app import MDApp
        from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
        from kivymd.uix.boxlayout import MDBoxLayout
        from kivy.uix.video import Video
    
        class TalkBot(Screen):
            def __init__(self, **kwa):
                super(TalkBot, self).__init__(**kwa)
                self.bgbox = MDBoxLayout()
                self.bgvid = Video(source="speechvid.avi", allow_stretch=True, keep_ratio=False, size_hint_y=1)
                self.bgvid.state = "play"
                self.bgbox.add_widget(self.bgvid)
                self.add_widget(self.bgbox)
    
            def on_enter(self):
                self.bgvid.state = "play"
    
            def on_leave(self):
                self.bgvid.unload()
    
        class MindMagic(MDApp):
            def build(self):
                self.theme_cls.material_style = "M3"
                self._app_name = "MindMagic"
                self.icon = "logo.png"
                sm = ScreenManager(transition=FadeTransition())
                sm.add_widget(Screen(name="winvid"))
                sm.add_widget(Screen(name="homeScreen"))
                sm.add_widget(TalkBot(name="talkbot"))
                return sm
    
        if __name__ == "__main__":
           MindMagic().run()