I am making a menu func which adds songs to the player.
def add_song():
song = filedialog.askopenfilename(initialdir='C:\\Users\\Soham\\Music', title="Choose a
song!",filetypes=(("mp3 Files", "*.mp3"), ))
song_name = song.split("/")[-1].split(".")[0]
song_list.insert(END, song_name)
Then afterwards I have a play button which is coded to play the song added -
play_button = Button(controls_frame, image=play_button_img,borderwidth=0, command = play)
play_button.grid(row=0,column=2,padx=5)
So, the func, play()'s code is -
def play():
song = song_list.get(ACTIVE)
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
But here dong variable in play() is actually just the name of song as it is already split off in add_song().And pygame needs the entire path as the song is not in the same directory as the python file. So pygame cannot open and play the song resulting in the error -
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Soham\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line
1885, in __call__
return self.func(*args)
File "c:\Users\Soham\Desktop\HM MP.py", line 26, in play
pygame.mixer.music.load(song)
pygame.error: Couldn't open 'Avicii - The Nights'
So what can I do about this , is there another way where I can split off the path for displaying the song name creating no problems for pygame to play the music??
Also, I am using Windows 10 Pro , high end machine and using Python 3.