How to fix this AttributeError about a missing method?

78 views Asked by At

I am having problems with running my music player script, maybe you can tell me what I did wrong?

Error:

AttributeError: 'MusicPlayer' object has no attribute 'pause'

Code:

from tkinter import *
from tkinter import filedialog
from pygame import mixer


class MusicPlayer:
    def __init__(self, window ):
        window.geometry('420x200'); window.title('MNote Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60) 
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()
    def play(self):
        if self.music_file:
            mixer.init()
            mixer.music.load(self.music_file)
            mixer.music.play()
        else:
            mixer.music.unpause()
            self.playing_state = False
    def stop(self):
        mixer.music.stop()
root = Tk()
app= MusicPlayer(root)
root.mainloop()
2

There are 2 answers

1
Mureinik On BEST ANSWER

Your MusicPlayer class really doesn't have a pause method.

You could create one by delegating to the underlying mixer:

def pause(self):
    mixer.music.pause()
0
rasool tahaei On

This is occurring because you have a file named MusicPlayer.py in your project which is shadowing the MusicPlayer module from Python's standard library. Please rename that file and the issue should go away.