Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been trying off and on for two weeks to figure this out with no luck. This is the first time in my coding learning journey i've felt actually completely stuck. Any help is incredibly appreciated.

Right now my issue is changing screens outside of my .kv file. Right now I am trying to use:

 class RootScreen(Screen):
    def onNextScreen(self, btn, fileName):
        MDApp.get_running_app().root.current = "ScreenVideo1"
        MDApp.get_running_app().root.ScreenVideo1.test_on_enter(r'C:filelocation' + fileName + '.MOV')  

But that gives the error code:

  'RootScreen' object has no attribute 'ScreenVideo1'

main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from kivy.uix.videoplayer import VideoPlayer
from kivy.uix.actionbar import ActionBar
from kivy.uix.button import Button
from kivy.core.window import Window

#remove after dev
Window.size = (300,500)

Window.clearcolor = (.976, .980, .996, 1)

screen_helper = r"""
RootScreen:
    MDNavigationLayout:
        ScreenManager:
            id: screen_manager
            ScreenHome:
                id: screen_home
                manager: screen_manager
                name: "Home"
                ScrollView:
                    pos_hint: {'bottom':1}
                    do_scroll_y: True
                    BoxLayout:
                        orientation: 'vertical'
                        padding: 15
                        spacing: 20
                        #size_hint: None, None
                        height: self.minimum_height
                        size_hint: (1, 2)
                        
                        MDToolbar:
                            title: "POTUS"
                            left_action_items:[["menu",lambda x: nav_drawer.set_state("open")]]
                            elevation: 8
                        Widget:
                        
                        StrokeButton:
                            text: "Video Category 1"
                            back_color: (0.576, 0.698, 0.996, 1) if self.state == 'normal' else (0.502, 0, 0.502, .75)
                            on_release: screen_manager.current = "Video1"
         
            ScreenAbout:
                id: screen_about
                manager: screen_manager
                name: "About"
                MDLabel:
                    text: "About"
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "POTUS"
                        left_action_items:[["menu",lambda x: nav_drawer.set_state("open")]]
                        elevation: 8
                    Widget:

            ScreenHelp:
                id: screen_help
                manager: screen_manager
                name: "Help"
                MDLabel:
                    text: "Help"
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "POTUS"
                        left_action_items:[["menu",lambda x: nav_drawer.set_state("open")]]
                        elevation: 8
                    Widget:

                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "POTUS"
                        left_action_items:[["menu",lambda x: nav_drawer.set_state("open")]]
                        elevation: 8
                    Widget:

            ScreenVideo1:
                id: screen_video1
                manager: screen_manager
                name: "Video1"
                GridLayout:
                    pos_hint: {'top': 1}
                    cols:1
                    rows:5
                    ActionBar:
                        pos_hint: {'top': 1}
                        height:'100sp'
                        ActionView:
                            ActionPrevious:
                                with_previous: True
                                on_release: screen_manager.current = "Home"
                    
                    StrokeButton:
                        text: " Video option 1"
                        back_color: (0.576, 0.698, 0.996, 1) if self.state == 'normal' else (0.502, 0, 0.502, .75)
                        on_release: root.onNextScreen(self, 'heart')
                    StrokeButton:
                        text: "Video option 2"
                        back_color: (0.576, 0.698, 0.996, 1) if self.state == 'normal' else (0.502, 0, 0.502, .75)
                        on_release:
                    StrokeButton:
                        text: "Video option 3"
                        back_color: (0.576, 0.698, 0.996, 1) if self.state == 'normal' else (0.502, 0, 0.502, .75)
                        on_release:   
    MDNavigationDrawer:
        id: nav_drawer
        BoxLayout: 
            orientation: 'vertical'
            spacing: '8dp'
            padding: '8dp'
                
                MDLabel:
                    text: "blah blah blah"
                    font_style: 'Subtitle1'
                    size_hint_y: None
                    height: self.texture_size[1]
                MDLabel:
                    text: "blah blah blah"
                    font_style: "Caption"
                    size_hint_y: None
                    height: self.texture_size[1]
                ScrollView: 
                    MDList:
                        OneLineIconListItem:
                            on_release:
                                screen_manager.current = "Home"
                            text: 'Home'
                            IconLeftWidget:
                                icon:"folder-home"
                                on_release:
                                    screen_manager.current = "Home"
                        OneLineIconListItem:
                            on_release:
                                screen_manager.current = "Help"
                            text: 'Help'
                            IconLeftWidget:
                                icon:"help-circle-outline"
                                on_release:
                                    screen_manager.current = "Help"
                        OneLineIconListItem:
                            on_release:
                                screen_manager.current = "About"
                            text: 'About'
                            IconLeftWidget:
                                icon: "doctor"
                                on_release:
                                    screen_manager.current = "About"

<StrokeButton@Button>:
    background_color:(0,0,0,0)
    background_normal:''
    back_color:(1,0,1,1)
    border_radius: 18
    color: self.back_color
    bold: True
    canvas.before:
        Color:
            rgba: self.back_color
        Line:
            rounded_rectangle: (self.pos[0],self.pos[1],self.size[0],self.size[1],self.border_radius)
            width: 1.2
"""

class RootScreen(Screen):
    def onNextScreen(self, btn, fileName):
        MDApp.get_running_app().root.current = "ScreenVideo1"
        MDApp.get_running_app().root.ScreenVideo1.test_on_enter(r'C:filelocation' + fileName + '.MOV')

class ScreenHome(Screen):
    pass

class ScreenAbout(Screen):
    pass

class ScreenHelp(Screen):
    pass

class ScreenVideo1(Screen):
    def test_on_enter(self, vidname):
        #self.add_widget(Button(text="Back"))
        self.vid = VideoPlayer(source=vidname, state='play',
                               options={'allow_stretch':True,
                                        'eos': 'loop'})
        self.add_widget(self.vid)

    def on_leave(self):
        pass

    def onBackBtn(self):
        self.vid.state = 'stop'
        self.remove_widget(self.vid)
        MDApp.get_running_app().root.current = "ScreenVideo1"
        
class DemoApp(MDApp):
    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen


DemoApp().run()

It used to be very easy when I was building my app with Manager

class Manager(ScreenManager):
    transition = NoTransition()
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)
    screen_home = ObjectProperty(None)
    

    def __init__(self, *args, **kwargs):
        super(Manager, self).__init__(*args, **kwargs)
        # list to keep track of screens we were in
        self.list_of_prev_screens = []

class ScreensApp(MDApp):
    def build(self):
        return Manager()

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

And then I would just change screens using manager. But now that I am using the navigation drawer I can no longer build my app with manager.

class ScreenOne(Screen):
    def onNextScreen(self, btn, fileName):
        self.manager.list_of_prev_screens.append(btn.parent.name)
        self.manager.current = 'screen2'
        self.manager.screen_two.test_on_enter(r'C:filelocation' + fileName + '.MOV')
question from:https://stackoverflow.com/questions/65949169/kivy-incorporating-video-to-navigation-drawer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code:

class RootScreen(Screen):
    def onNextScreen(self, btn, fileName):
        MDApp.get_running_app().root.current = "ScreenVideo1"
        MDApp.get_running_app().root.ScreenVideo1.test_on_enter(r'C:filelocation' + fileName + '.MOV')

is trying to treat the root widget of the App as a ScreenManager, but it isn't, it is a RootScreen. So, you need to access the ScreenManager, like this:

class RootScreen(Screen):
    def onNextScreen(self, btn, fileName):
        MDApp.get_running_app().root.ids.screen_manager.current = "Video1"
        MDApp.get_running_app().root.ids.screen_manager.current_screen.test_on_enter(r'C:filelocation' + fileName + '.MOV')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...