首页 > 解决方案 > Multiple screens but self.root.ids retuns an error

问题描述

This code actually worked until I added multiple screens. It still works when I remove the multi Screen Manager. I get the attrubite error point to Chapter ids Im trying to add a chapters screen. What Im I doing wrong please.

I think the error is from self.root.ids. I may be wrong. Below is thwe code

Python

from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.button import MDRectangleFlatIconButton, MDFloatingActionButton
from kivy.lang import Builder
import pygame

pygame.mixer.init()
path = "C://abapp3"


class BooksScreen(Screen):
    pass


class ChapterScreen(Screen):
    pass


class MainApp(MDApp):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(BooksScreen(name='BooksScreen1'))
        sm.add_widget(ChapterScreen(name='ChapterScreen1'))

    return sm

def on_start(self):
    songs = self.load_songs(path)
    pygame.mixer.music.load(songs[0])

def load_songs(self, path_):
    songs = []
    for filename in os.listdir(path_):
        if filename.endswith('.wav'):
            songs.append(os.path.join(path_, filename))
            self.root.ids.Chapter.add_widget(OneLineListItem(text=filename[2:],
                                                         on_release=self.play_song,
                                                         pos_hint={"center_x": 1, "center_y": 1}, ))

    return songs

@staticmethod
def play_song(*args):
    pygame.mixer.music.play()
    print(OneLineListItem.text)

@staticmethod
def stop_song(*args):
    pygame.mixer.music.stop()
    print("song stopped")

MainApp().run()

.KV

ScreenManager:
    Screen
        BooksScreen:
        ChapterScreen:



<BooksScreen>:
    NavigationLayout:
        ScreenManager:
            Screen:
                # Front / Main Screen
                MDBoxLayout:
                    orientation: "vertical"
                   #Toolbar
                    MDToolbar:
                        title: "Chapters"
                        font_style: "Caption"
                        elevation: 8
                        left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]

                    Widget:

                #stop/pause Button
                MDBoxLayout:
                    orientation:"vertical"
                #    id: StopButton
                    text:"Pause"

                BoxLayout:
                    Button:
                        text: 'Goto settings'
                        on_press:
                            root.manager.transition.direction = 'left'
                            root.manager.current = 'ChapterScreen1'





                # Chapters list/ Play list
                MDScreen
                    MDBoxLayout:
                        orientation: "vertical"
                        MDList
                            id: Chapter



        #Options menu
        MDNavigationDrawer:
            id: nav_drawer
            MDBoxLayout:
                orientation: "vertical"
                padding: "8dp"
                spacing: "8dp"




                ScrollView:
                    # Options Menu Options
                    MDList:
                        MDRectangleFlatIconButton:
                            on_press:
                                root.ids.nav_drawer.set_state("close")

                            pos_hint: {"center_x": .5, "center_y": .5}
                            icon: 'arrow-left'
                            line_color: 0, 0, 0, 0




                        OneLineIconListItem:
                            text: "Options"
                            font_style: "Caption"
                            #size_hint_y: None
                            #height: self.texture_size[1]

                         # Options Menu- About
                        OneLineIconListItem:
                            text: "About"
                            font_style: "Caption"
                           # size_hint_y: None
                            #height: self.texture_size[1]

                        # Options Menu Storage
                        OneLineIconListItem
                            text: 'Storage'
                            font_style: "Caption"
                            #IconLeftWidget
                                #icon: 'tools'

                        # Options Menu Toolbox
                        OneLineIconListItem:
                            text: 'Choose Voice'
                            font_style: "Caption"

                            #IconLeftWidget:
                            #    icon: 'toolbox'

                        # Options Menu About
                        OneLineIconListItem:
                            text: 'About'
                            font_style: "Caption"
                           # IconLeftWidget:
                            #    icon: 'toolbox-outline'
<ChapterScreen>:
    BoxLayout:
        Button:
            text: 'Back to menu'
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'BooksScreen1'

标签: pythonpython-3.xkivykivymd

解决方案


该行:

        self.root.ids.Chapter.add_widget(OneLineListItem(text=filename[2:],
                                                     on_release=self.play_song,
                                                     pos_hint={"center_x": 1, "center_y": 1}, ))

试图引用 ,Chapter id就好像它是ScreenManager( root)的成员一样ids,但事实并非如此。它是 的ids成员BookScreen。您可以通过使用以下方法引用BookScreen问题行中的实例来修复该错误:get_screen()ScreenManager

        self.root.get_screen('BooksScreen1').ids.Chapter.add_widget(OneLineListItem(text=filename[2:],
                                                     on_release=self.play_song,
                                                     pos_hint={"center_x": 1, "center_y": 1}, ))

推荐阅读