from tkinter import *
import pygame
from tkinter import filedialog
root = Tk()
root.title('bynet mp3 player')
root.geometry("500x300")
# Initialze Pygame Mixer
pygame.mixer.init()
#Add Song Function
def add_song():
song = filedialog.askopenfilenames(initialdir='audio/', title="Choose A Song", filetypes=(("mp3 Files", "*.mp3"), ))
#strip out the directory info and .mp3 extension from the song name
song = song.replace("serverlow@serverlow:~/music/audio", "")
song = song.replace(".mp3", "")
# Add song to listbox
song_box. insert(END, song)
# Create Playlist Box
song_box = Listbox(root, bg="black", fg="green", width=60)
song_box.pack(pady=20)
# Define Player Control Buttons Images
back_btn_img = PhotoImage(file='image/back50.png')
forward_btn_img = PhotoImage(file='image/forward50.png')
play_btn_img = PhotoImage(file='image/play50.png')
pause_btn_img = PhotoImage(file='image/pause50.png')
stop_btn_img = PhotoImage(file='image/stop50.png')
# Create Player Control Frames
controls_frame = Frame(root)
controls_frame.pack()
# Create Player Control Buttos
back_button = Button(controls_frame, image=back_btn_img, borderwidth=0)
forward_button = Button(controls_frame, image=forward_btn_img, borderwidth=0)
play_button = Button(controls_frame, image=play_btn_img, borderwidth=0)
pause_button = Button(controls_frame, image=pause_btn_img, borderwidth=0)
stop_button = Button(controls_frame, image=stop_btn_img, borderwidth=0)
back_button.grid(row=0, column=0 , padx=10)
forward_button.grid(row=0, column=1, padx=10)
play_button.grid(row=0, column=2, padx=10)
pause_button.grid(row=0, column=3, padx=10)
stop_button.grid(row=0, column=4, padx=10)
# Create Menu
my_menu = Menu(root)
root.config(menu=my_menu)
# Add Add Song Menu
add_song_menu = Menu(my_menu)
my_menu.add_cascade(label="Add Song", menu=add_song_menu)
add_song_menu.add_command(label="Add One Song To Playlist", command=add_song)
root.mainloop()
----------------------------------------------------------------
Error:
serverlow@serverlow:~/music$ python3 music.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "music.py", line 20, in add_song
song = song.replace("serverlow@serverlow:~/music/audio", "")
AttributeError: 'tuple' object has no attribute 'replace