无法将我的BoxLayout添加到ScrollView。Error:'已经有父级'

2024-10-08 21:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将BoxLayout添加到ScollView,但我不能

我有一个ScreenManager()管理两个屏幕:MainScreen()和Songs(),在Songs下,我有BoxLayout()和ScrollView(),在init下。你知道吗

>;要在屏幕间查看歌曲(>);screenView()=>;关系管理器(>)。你知道吗

请记住,我不能使用生成器或.kv文件,因为创建的按钮是动态的。你知道吗

另外,我留下了所有的'定义'的歌曲类的代码,但我有他们。你知道吗

代码如下:


class Mainscreen(Screen):

    def __init__(self, **kwargs):
        super(Mainscreen,self).__init__(**kwargs)
        self.layoutMs = BoxLayout(orientation='vertical')
        self.songButton = Button(text='Songs')
        self.pressed=False
        self.layoutMs.add_widget(Label(text='Welcome To CarsonMusic!'))
        self.songButton.bind(on_press=self.switch_to_songs)
        self.layoutMs.add_widget(self.songButton)
        self.add_widget(self.layoutMs)
    def switch_to_songs(self, *args):
        self.manager.current='song_screen'

class Songs(Screen):#Creates MainScreen class as widget
    titleP = ''
    current_artist=''

    def __init__(self, **kwargs):#Initializes at the beginning whenever mainscreen is called no matter what def
        super(Songs, self).__init__(**kwargs)
        self.Lsong = []#sets default List of song
        self.Lartist = []#sets default List of artists
        self.LsongFil = []#sets default List of files
        self.filebase= os.getcwd()#sets filebase to the original directory DOES NOT NAVIGATE TO MUSIC FOLDER YET
        self.filebaseM = (str(self.filebase)+"/Music/")#Sets filebase of music to music folder in orig directory
        print (self.filebaseM)
        #self.filebaseM2 = filebaseM.replace("/","\\'")
        self.listfil = os.listdir(self.filebaseM)#Gets list of files/folders in Music directory
        self.listfil = [phrase.replace('.mp3', '') for phrase in self.listfil] #removes .mp3 from filename
        self.Lsong=self.listfil#sets new List of songs
        self.song_select_layout = BoxLayout(orientation='vertical')  # Makes layout boxlayout with vertical orientation
        self.song_select_layout.buttons = []  # states that the buttons will be in a list
        self.back_button=Button(text='Back')
        self.back_button.bind(on_press=self.back_to_main)
        self.song_select_layout.add_widget(self.back_button)
        self.np_text=Label(text='Song Playing Now Is')
        self.np_song=Label()
        self.np_Atext=Label(text='By')
        self.np_artist=Label()


        root = ScrollView()
        root.add_widget(self.song_select_layout)
        self.add_widget(root)


        self.curr_art = "Default"
        for song in self.Lsong:  # for loop
            button = Button(text=song, font_size=20, color=(0, 0, 0, 1))  # defines that button is making a button with text of value of song on it
            button.bind(on_press=self.songPlay)  # binds the button to navigate to songPlay method when pressed
            self.song_select_layout.buttons.append(button)  # makes is so that each element of list is appended to a button
            self.song_select_layout.add_widget(button)  # adds the buttons to the layout
        self.add_widget(self.song_select_layout)
        self.song_select_layout.add_widget(self.np_text)
        self.song_select_layout.add_widget(self.np_song)
        self.song_select_layout.add_widget(self.np_Atext)
        self.song_select_layout.add_widget(self.np_artist)

class CarsonMusic(App):
    def build(self):
        sm = ScreenManager()
        main_screen = Mainscreen(name='main_screen')
        song_screen = Songs(name='song_screen')
        sm.add_widget(main_screen)
        sm.add_widget(song_screen)

        return sm

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

错误如下:

 Traceback (most recent call last):
   File "C:/Users/user/Desktop/CarsonMusic/CarsonApp.py", line 170, in <module>
     CarsonMusic().run()
   File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\app.py", line 829, in run
     root = self.build()
   File "C:/Users/user/Desktop/CarsonMusic/CarsonApp.py", line 163, in build
     song_screen = Songs(name='song_screen')
   File "C:/Users/user/Desktop/CarsonMusic/CarsonApp.py", line 78, in __init__
     self.add_widget(self.song_select_layout)
   File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\floatlayout.py", line 140, in add_widget
     return super(FloatLayout, self).add_widget(widget, index, canvas)
   File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\layout.py", line 97, in add_widget
     return super(Layout, self).add_widget(widget, index, canvas)
   File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kivy\uix\widget.py", line 623, in add_widget
     % (widget, parent))
 kivy.uix.widget.WidgetException: Cannot add <kivy.uix.boxlayout.BoxLayout object at 0x040B4D18>, it already has a parent <kivy.uix.scrollview.ScrollView object at 0x0AACE6C0>

因为我的自用“对不起”


Tags: oftotextinselfaddsongnp
1条回答
网友
1楼 · 发布于 2024-10-08 21:20:08

您正在尝试将song_select_layout添加到ScrollViewSongs方法中:

root.add_widget(self.song_select_layout)

以及

self.add_widget(self.song_select_layout)

只能将Widget添加到一个父级。你知道吗

相关问题 更多 >

    热门问题