Today we will create a music player of our own in Python. To do this, we will use three packages:

  • Tkinter: used for UI

  • Pygame: Play music

  • OS: Used to access system files

Here we’ll look at each step of creating a music player, and we’ll break the code into four parts. In the first part, we will import all the packages. Second, we will create the UI. Third, we imported all the music from the system and displayed it in the APP. Fourth, design a feature that helps you play specific music.

We must install all three packages on our system before importing them.

pip install pygame
pip install tkinter
Copy the code

The OS is already installed, so we just need to import it:

from tkinter import *
from pygame import mixer
import os
Copy the code

Now we will create the UI for the application.

Root = Tk() root.title(" music player ") Button(root,text= "play", command = playsong). Grid (row =1, Column = 0) Button(root,text= "pause", command = pausesong). Grid (row =1, column =1) Button(root,text= "stop", Grid (row =1, column = 2) Button(root,text= "resume", command = "resumesong"). Grid (row =1, column = 3) mainloop()Copy the code

Here we create four buttons and pass commands, and in the next step, we’ll create a function for each function to help play, stop, resume, and pause a particular song. We will also store all songs in our playlist, so in step 3, we will create a variable name as a playlist and store and display the playlist from our system.

Mixer.init () playList = Listbox(root, selectMode =SINGLE,bg = "green") Playlist.grid (columnSPAN =5) OS. The chdir ('/Users/rajatupadhyaya/Documents/song) song = OS. Listdir () for s in song: playlist. Insert (END, s)Copy the code

Mixer.init () checks all current songs in the system. In the next line, we create a list box that will display in our UI. OS doesn’t help access a specific directory on the next line, so we give the path and store all the music in the song variable, and run a loop to store all the songs in the playlist. Now, we’ll define the functionality and create the playlist in the last step.

def playsong():
currentsong = playlist.get(ACTIVE)
print(currentsong)
mixer.music.load(currentsong)
mixer.music.play()
def pausesong():
mixer.music.pause()
def stopsong():
mixer.music.stop()
def resumesong():
mixer.music.unpause()
Copy the code

The playsong function checks the currently selected song and plays the song after loading it. Complete code:

The final music player APP is as follows:

How to obtain the source code:

① More than 3000 Python ebooks ②Python development environment installation tutorial ③Python400 set self-learning video ④ software development common vocabulary ⑤Python learning roadmap ⑤ project source code case sharing if you use it can be directly taken away in my QQ technical exchange group group number: 754370353 (pure technical exchange and resource sharing, no advertising) to take away by yourself click here to collect