简体   繁体   中英

AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal'

I am making a mp3 player in kivy and i am facing a problem. i have searched about the solution but none of them works for me. I am getting this error AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal' and i am unable to resolve it. please help me.

My python code

class PlaySongScreen(Screen):
    play_btn = ObjectProperty(None)
    def play(self, song_path):
        #function to play the desired song
        if song_path == 'None':
            song_path = songs_list[0]['text']
        mypath = song_path
        mypath = mypath.replace('\\', '\\\\')
        if pg.mixer.music.get_busy() == 0:
            #playing the song
            pg.mixer.music.load(mypath)
            pg.mixer.music.play() 

            #change the play button to pause button
            self.play_btn.background_normal = 'images\\new_pause.jpg' 
            self.play_btn.background_down = 'images\\pause.jpg'

play_function = PlaySongScreen.play

class PlayButton(Factory.Button):
    def on_press(self):
        play_function(PlaySongScreen, song_path=self.text)

My.kv code

<PlaySongScreen>:
    play_btn: play_pause
    GridLayout:
        cols: 1
        padding: 5
        RelativeLayout:
            Button:
                id: play_pause
                on_press: root.play('None')
                background_normal: 'images\\new_play.jpg'
                background_down: 'images\\play.jpg'
                size_hint: 0.8, 0.9
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}

error i am getting

File "c:/Users/user/Desktop/Coding/python/Projects/Music Player/music_player.py", line 126, in on_press
 play_function(PlaySongScreen, song_path=self.text)
 File "c:/Users/user/Desktop/Coding/python/Projects/Music Player/music_player.py", line 40, in play
 self.play_btn.background_normal = 'images\\new_pause.jpg'
 AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'background_normal'

Please help me resolve this error.

Cannot reproduce your error with the code you have posted. But I suspect that in your actual code you are using PlayButton , which calls PlaySongScreen.play . If that is what you are actually doing, then that is the cause of your problem. If you access PlaySongScreen.play , you are accessing the ObjectProperty , not the Button with id play_pause . You must access the play_btn attribute through the instance of PlaySongScreen , not the the class itself.

The way your posted code does it is correct. Using a Button with on_press set to root.play . The root is the instance of PlaySongScreen .

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