Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

Music Player in Tkinter Using Python

In this article, we will learn how to create a music player in Tkinter. Music Player used to listen to the music files stored in the memory.

Libraries Required:-

  1. Pygame
  2. OS (Comes with Python)
  3. Tkinter

Installation:-

pip install pygame

Approach:-

  1. First, we will select the folder which contains music files
  2. Then we select the music file which we want to listen
  3. We can do four operations; play, pause, unpause and stop the music
  4. Increase and decrease the volume
Below is how the GUI looks like:-

Let's Understand Step by Step Implementation:-

Step 1 (Import Required Library)

# Import Required Library

from pygame import mixer 
from tkinter import *
from tkinter import filedialog
import os
from tkinter import ttk

Step 2 (Select Music Folder)

Now, we will create a separate function named select_folder which will ask for the folder/directory which contains music files.

In Tkinter, we have an in-built method named as askdirectory() which will ask for the folder

def select_folder():
  global files
  folder_name = filedialog.askdirectory()

  os.chdir(folder_name)
  files = os.listdir()

  for file in files:
    list_box.insert(END, file)
  

Step 3 (Music Player Operation)

In a Music Player, we can do the following thing:-

  1. Playing the Music
  2. Pause the Music
  3. Control the Volume
  4. Unpause the Music
  5. Stop the Music

play_music():- It will play the selected music

For playing the music, we will use the play() method in the pygame module

pause_music():- It will pause the music

For playing the music, we will use the pause() method in the pygame module

unpause_music():- It will unpause the music

For playing the music, we will use the unpause() method in the pygame module

stop_music():- It will stop the music

For playing the music, we will use the stop() method in the pygame module

volume_controller():- It is used to control the volume of the music

For playing the music, we will use the set_volume() method in the pygame module. value is between 0.0 to 1.0

def play_music():
  song_name = files[list_box.curselection()[0]]
  mixer.music.load(song_name)
  mixer.music.play() 

def pause_music():
  mixer.music.pause()

def unpause_music():
  mixer.music.unpause() 

def stop_music():
  mixer.music.stop()

def volume_controller(x):
    mixer.music.set_volume(int(width_slider.get())/10)
  

Step 4 (GUI Implementation)

# Create Tkinter Object
root = Tk()

# Initialize pygame
mixer.init() 

# set Geomtery
root.geometry("400x400")

# Add Labels, Buttons
Label(text="Music Player", font="italic 15 bold").pack(pady=5)

Button(text="Select Folder",command=select_folder).pack(pady=5)

# Add scollbar
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT, fill = BOTH)
list_box = Listbox(root)
list_box.pack(expand = YES, fill = "both") 
list_box.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = list_box.yview) 

# Add Multiple Frames
frame = Frame()
frame.pack()
frame1 = Frame()
frame1.pack(pady=10)

Label(frame,text="Cotrol Panel", font="italic 15 bold",fg="red").pack(pady=5,side=LEFT)

# Add Scale bar 
width_slider = ttk.Scale(frame, 
                        from_ = 1, 
                        to = 10, 
                        orient = HORIZONTAL, 
                        length = 100, value = 0,
                        command=volume_controller) 

width_slider.pack(pady = 10, padx = 20) 


play = Button(frame1,text="Play",width=10,relief="solid",fg="red",command=play_music)
play.pack(side=LEFT)

pause = Button(frame1,text="Pause",width=10,relief="solid",fg="blue",command=pause_music)
pause.pack(side=LEFT,padx=10)

unpause = Button(frame1,text="Unpause",width=10,relief="solid",fg="green",command=unpause_music)
unpause.pack(side=LEFT)

stop = Button(frame1,text="Stop",width=10,relief="solid",fg="orange",command=stop_music)
stop.pack(padx=5)

# Execute Tkinter
root.mainloop()

Below is the Implementation:-

# Import Required Library
from pygame import mixer 
from tkinter import *
from tkinter import filedialog
import os
from tkinter import ttk

# Create Tkinter Object
root = Tk()

# Initialize pygame
mixer.init() 

# set Geomtery
root.geometry("400x400")

def select_folder():
  global files
  folder_name = filedialog.askdirectory()

  os.chdir(folder_name)
  files = os.listdir()

  for file in files:
    list_box.insert(END, file)


def play_music():
  song_name = files[list_box.curselection()[0]]
  mixer.music.load(song_name)
  mixer.music.play() 

def pause_music():
  mixer.music.pause()

def unpause_music():
  mixer.music.unpause() 

def stop_music():
  mixer.music.stop()

def volume_controller(x):
    mixer.music.set_volume(int(width_slider.get())/10)


# Add Labels, Buttons
Label(text="Music Player", font="italic 15 bold").pack(pady=5)

Button(text="Select Folder",command=select_folder).pack(pady=5)

# Add scollbar
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT, fill = BOTH)
list_box = Listbox(root)
list_box.pack(expand = YES, fill = "both") 
list_box.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = list_box.yview) 

# Add Multiple Frames
frame = Frame()
frame.pack()
frame1 = Frame()
frame1.pack(pady=10)

Label(frame,text="Cotrol Panel", font="italic 15 bold",fg="red").pack(pady=5,side=LEFT)

# Add Scale bar 
width_slider = ttk.Scale(frame, 
                        from_ = 1, 
                        to = 10, 
                        orient = HORIZONTAL, 
                        length = 100, value = 0,
                        command=volume_controller) 

width_slider.pack(pady = 10, padx = 20) 


play = Button(frame1,text="Play",width=10,relief="solid",fg="red",command=play_music)
play.pack(side=LEFT)

pause = Button(frame1,text="Pause",width=10,relief="solid",fg="blue",command=pause_music)
pause.pack(side=LEFT,padx=10)

unpause = Button(frame1,text="Unpause",width=10,relief="solid",fg="green",command=unpause_music)
unpause.pack(side=LEFT)

stop = Button(frame1,text="Stop",width=10,relief="solid",fg="orange",command=stop_music)
stop.pack(padx=5)

# Execute Tkinter
root.mainloop()

Output:-