Music Player App using Python
Introduction
- This project is about creating a music player app using Python.
- It involves designing a UI with buttons and song lists, and implementing functionality using the Mixer module from Pygame.
UI Design
- The window is divided into three parts:
- Logo Section: Where a music icon will be displayed.
- Song List Section: Display a list of songs.
- Button Section: Include buttons like play, pause, rewind, next song, stop, and continue.
- Parameters for the window: width = 352, height = 255, background color = white.
- Use
color1
, color3
for defining color schemes in the application.
- Create frames for each section (left frame, right frame, and down frame) and set dimensions.
Adding Images and Icons
- Use the Pillow module to load and manipulate images.
- Icons are loaded from an 'icons' folder; each button has a corresponding icon (e.g., play.png, stop.png).
- Resize the icons to fit the UI layout.
Example Code Snippet
from PIL import Image, ImageTk
img = Image.open('icons/play.png')
img = img.resize((30, 30))
img = ImageTk.PhotoImage(img)
Playlist Section
- Use a Listbox to display the songs.
- The playlist is populated from a directory of songs using the
os
module.
- Scrollbar is attached to the Listbox for scrolling through the songs.
- Demo songs can be inserted for visualization.
Example Code Snippet
import os
os.chdir('path_to_music_folder')
songs = os.listdir()
for song in songs:
listbox.insert('end', song)
Playing Music
- Load and play songs using the Mixer module from Pygame.
- Functions for various controls: play, pause, continue, stop, next song, and previous song.
Example Code Snippet
from pygame import mixer
mixer.init()
def play_music():
current_song = listbox.get('active')
mixer.music.load(current_song)
mixer.music.play()
label.config(text=current_song)
Control Events
- Play Music: Load and play the selected song.
- Pause Music: Pause the currently playing song.
- Continue Music: Continue a paused song.
- Stop Music: Stop the music playback.
- Next Song: Jump to the next song in the list.
- Previous Song: Jump to the previous song in the list.
Example Code Snippet
def next_music():
current_song = listbox.get('active')
current_index = songs.index(current_song)
next_index = (current_index + 1) % len(songs)
next_song = songs[next_index]
mixer.music.load(next_song)
mixer.music.play()
listbox.selection_clear(0, 'end')
listbox.activate(next_index)
listbox.selection_set(next_index)
label.config(text=next_song)
Conclusion
- The application effectively demonstrates handling GUIs with Tkinter and controlling music playback with the Pygame Mixer module.
- The project showcases dividing a window using frames, loading images, handling events for buttons, and managing playlists.
Thank you for watching! If you enjoyed this project, please like and subscribe to the channel for more exciting Python projects.