Today we’ll use Python to create a GUI for our music player. There will be no more conversations; prepare with your interpreter!
Why do I want it? To learn and boost my Python skills.
Python Fundamentals knowledge
Tkinter and PyGame should be installed. After that, we must import modules.
pip install pygame
Code with explanation:
<iframe src="https://medium.com/media/4f91af0b7b49028740860fc04f8e2a0f" frameborder=0></iframe>
<iframe src="https://medium.com/media/ab6a3d9611cd71bb277d99dd41cfd5fd" frameborder=0></iframe>
The music is played using the playsong() method. It plays the needed music after loading the active song from the list. When the user presses the “play” button, it is activated.
The currentsong() function prints the currently playing song/music in the PlayList. It sets ‘Playing’ as the song state.
<iframe src="https://medium.com/media/60af006f395c064f67d4692a927f457a" frameborder=0></iframe>
<iframe src="https://medium.com/media/18c799e274d21f79eb0c6d6fc27be545" frameborder=0></iframe>
<iframe src="https://medium.com/media/ef98d1b6732377a4372347847f096274" frameborder=0></iframe>
These are also GUI-related. The letters ‘fg’ and ‘bg’ stand for foreground and backdrop, respectively.
playlist.grid() uses absolute row and column coordinates to place widgets in a two-dimensional grid.
<iframe src="https://medium.com/media/82da217f1bd4916307788573c679477f" frameborder=0></iframe>
In Python, the chdir() method is used to change the current working directory to a given path. As for the new directory path, it only accepts one option.
The Python listdr() function is used to acquire a list of all files and folders in a given directory. If we don’t provide a directory, the current working directory’s list of files and directories will be returned.
It adds all of the files in our file directory into the playlist in the ‘for’ loop.
<iframe src="https://medium.com/media/c7b77da2e7eac4ee0af52b45dacb71e9" frameborder=0></iframe>
This is the final section. These are all lines for GUI Buttons.
<iframe src="https://medium.com/media/76ec31805b1cc4041a8c3f8885c963ea" frameborder=0></iframe>
Finally, you’ve created your Python music player! Congratulations, friend!
Since we want only the music player, don’t care much about GUI.😉
I hope you enjoy this modest music player with few options. Don’t forget to applaud for this article and follow me.
Thank you for reading!
More content at plainenglish.io