import os
import tkinter as tk
from tkinter import filedialog, messagebox
import pygame
class MusicPlayer:
def __init__(self, root):
self.root = root
self.root.title("Music Playlist Manager 🎵")
self.root.geometry("500x400")
pygame.init()
pygame.mixer.init()
self.playlist = []
self.current_song_index = -1
self.is_paused = False
# GUI Components
self.playlist_box = tk.Listbox(root, selectmode=tk.SINGLE, bg="white", fg="black", width=60, height=15)
self.playlist_box.pack(pady=10)
btn_frame = tk.Frame(root)
btn_frame.pack()
tk.Button(btn_frame, text="Add Song", command=self.add_song).grid(row=0, column=0, padx=5)
tk.Button(btn_frame, text="Remove Song", command=self.remove_song).grid(row=0, column=1, padx=5)
tk.Button(btn_frame, text="Play", command=self.play_song).grid(row=0, column=2, padx=5)
tk.Button(btn_frame, text="Pause", command=self.pause_song).grid(row=0, column=3, padx=5)
tk.Button(btn_frame, text="Resume", command=self.resume_song).grid(row=0, column=4, padx=5)
tk.Button(btn_frame, text="Stop", command=self.stop_song).grid(row=0, column=5, padx=5)
self.current_song_label = tk.Label(root, text="Now Playing: None", fg="blue", font=("Arial", 10, "bold"))
self.current_song_label.pack(pady=10)
def add_song(self):
file_path = filedialog.askopenfilename(filetypes=[("MP3 Files", "*.mp3")])
if file_path:
self.playlist.append(file_path)
self.playlist_box.insert(tk.END, os.path.basename(file_path))
def remove_song(self):
selected_index = self.playlist_box.curselection()
if selected_index:
index = selected_index[0]
self.playlist_box.delete(index)
del self.playlist[index]
def play_song(self):
selected_index = self.playlist_box.curselection()
if selected_index:
index = selected_index[0]
self.current_song_index = index
song_path = self.playlist[index]
pygame.mixer.music.load(song_path)
pygame.mixer.music.play()
self.is_paused = False
self.current_song_label.config(text=f"Now Playing: {os.path.basename(song_path)}")
def pause_song(self):
if not self.is_paused:
pygame.mixer.music.pause()
self.is_paused = True
def resume_song(self):
if self.is_paused:
pygame.mixer.music.unpause()
self.is_paused = False
def stop_song(self):
pygame.mixer.music.stop()
self.current_song_label.config(text="Now Playing: None")
# Run the application
if __name__ == "__main__":
root = tk.Tk()
app = MusicPlayer(root)
root.mainloop()
No comments:
Post a Comment