Music Player
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pygame
import os
import random
# Initialize Pygame mixer
[Link]()
class MusicPlayer:
def __init__(self, root):
[Link] = root
[Link]("Music Player")
[Link]("600x400")
[Link] = []
self.current_index = 0
self.is_paused = False
self.is_shuffled = False
self.is_repeating = False
self.create_widgets()
def create_widgets(self):
self.playlist_box = [Link]([Link], selectmode=[Link], width=60, height=15)
self.playlist_box.pack(pady=20)
controls_frame = [Link]([Link])
controls_frame.pack()
self.play_button = [Link](controls_frame, text="Play", command=self.play_music)
self.play_button.grid(row=0, column=0, padx=10)
self.pause_button = [Link](controls_frame, text="Pause",
command=self.pause_music)
self.pause_button.grid(row=0, column=1, padx=10)
self.stop_button = [Link](controls_frame, text="Stop", command=self.stop_music)
self.stop_button.grid(row=0, column=2, padx=10)
self.next_button = [Link](controls_frame, text="Next", command=self.next_music)
self.next_button.grid(row=0, column=3, padx=10)
self.prev_button = [Link](controls_frame, text="Previous",
command=self.prev_music)
self.prev_button.grid(row=0, column=4, padx=10)
self.shuffle_button = [Link](controls_frame, text="Shuffle",
command=self.toggle_shuffle)
self.shuffle_button.grid(row=0, column=5, padx=10)
self.repeat_button = [Link](controls_frame, text="Repeat",
command=self.toggle_repeat)
self.repeat_button.grid(row=0, column=6, padx=10)
self.volume_slider = [Link](controls_frame, from_=0, to=1, orient=[Link],
command=self.set_volume)
self.volume_slider.set(0.5)
self.volume_slider.grid(row=1, columnspan=7, pady=20)
menu_bar = [Link]([Link])
[Link](menu=menu_bar)
add_song_menu = [Link](menu_bar, tearoff=0)
menu_bar.add_cascade(label="Add Songs", menu=add_song_menu)
add_song_menu.add_command(label="Add Songs to Playlist",
command=self.add_songs)
def add_songs(self):
songs = [Link](title="Choose Songs", filetypes=(("MP3 Files",
"*.mp3"), ("All Files", "*.*")))
for song in songs:
song_name = [Link](song)
self.playlist_box.insert([Link], song_name)
[Link](song)
def play_music(self):
if not [Link]:
[Link]("No Songs", "Please add songs to the playlist.")
return
if self.is_paused:
[Link]()
self.is_paused = False
else:
[Link]([Link][self.current_index])
[Link]()
def pause_music(self):
[Link]()
self.is_paused = True
def stop_music(self):
[Link]()
def next_music(self):
self.stop_music()
if self.is_shuffled:
self.current_index = [Link](0, len([Link]) - 1)
else:
self.current_index = (self.current_index + 1) % len([Link])
self.play_music()
def prev_music(self):
self.stop_music()
self.current_index = (self.current_index - 1) % len([Link])
self.play_music()
def toggle_shuffle(self):
self.is_shuffled = not self.is_shuffled
status = "On" if self.is_shuffled else "Off"
[Link]("Shuffle", f"Shuffle is now {status}")
def toggle_repeat(self):
self.is_repeating = not self.is_repeating
[Link].set_endevent([Link])
status = "On" if self.is_repeating else "Off"
[Link]("Repeat", f"Repeat is now {status}")
def set_volume(self, val):
volume = float(val)
[Link].set_volume(volume)
def mainloop(self):
[Link]()
if __name__ == "__main__":
root = [Link]()
app = MusicPlayer(root)
# Event to handle repeat functionality
def check_repeat():
if app.is_repeating and not [Link].get_busy():
app.play_music()
[Link](1000, check_repeat)
check_repeat()
[Link]()