How to create a music player app using python
python music playerpython music player project
python music library
python music bot
python music projects
python music generation
python music player report
python music module
Tkinter
We had already told you among the title of this page that we've got a bent to unit of measurement reaching to use the Tkinter library, that would be a traditional library for graphical user interface creation.
from tkinter import *
The Tkinter library is most well likable and very simple to use and it comes with many widgets Pygame may well be a Python module that works with tricks and sound libraries and designed with the ability of wiggling with entirely completely different transmission formats like audio, video, etc. whereas creating our Music Player application, we have a tendency to are reaching to be victimization Pygame's mixer.music module for providing entirely completely different usefulness to our music player application that is generally related to the manipulation of the song tracks.
import pygame
OS module
There is no have to be compelled to install this module expressly, as a result of it comes with the standard library of Python. This module provides entirely completely different functions for interaction with the package. MusicPlayer class
import OS
root = Tk()
MxPlayer(root)
Here we've the creator and additionally the various functions made public among the MxPlayer class.
1. init creator
With the help of this creator, we have a tendency to ar reaching to set the title for the window and maths for the window. we have a tendency to are reaching to initiate pygame and pygame mixer and then declare track variable and standing variable.
we have a tendency to are reaching to then turn out the Track Frames for Song label & standing label and then once Insert the Song Track Label and standing Label.
def __init__(self,root):
self.root = root
# Title of the window
self.root.title("MX palyer")
# Window Geometry
self.root.geometry("1000x200+200+200")
# Initiating Pygame
pygame.init()
# Initiating Pygame Mixer
pygame.mixer.init()
# Declaring track Variable
self.track = StringVar()
# Declaring Status Variable
self.status = StringVar()
# Creating the Track Frames for Song label & status label
trackframe = LabelFrame(self.root,text="Song Track",font=("times new roman",15,"bold"),bg="Navyblue",fg="white",bd=5,relief=GROOVE)
trackframe.place(x=0,y=0,width=600,height=100)
# Inserting Song Track Label
songtrack = Label(trackframe,textvariable=self.track,width=20,font=("times new roman",24,"bold"),bg="Orange",fg="gold").grid(row=0,column=0,padx=10,pady=5)
# Inserting Status Label
trackstatus = Label(trackframe,textvariable=self.status,font=("times new roman",24,"bold"),bg="orange",fg="gold").grid(row=0,column=1,padx=10,pady=5)
# Creating Button Frame
buttonframe = LabelFrame(self.root,text="Control Panel",font=("times new roman",15,"bold"),bg="grey",fg="white",bd=5,relief=GROOVE)
buttonframe.place(x=0,y=100,width=600,height=100)
# Inserting Play Button
playbtn = Button(buttonframe,text="PLAYSONG",command=self.playsong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=0,padx=10,pady=5)
# Inserting Pause Button
playbtn = Button(buttonframe,text="PAUSE",command=self.pausesong,width=8,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=1,padx=10,pady=5)
# Inserting Unpause Button
playbtn = Button(buttonframe,text="UNPAUSE",command=self.unpausesong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=2,padx=10,pady=5)
# Inserting Stop Button
playbtn = Button(buttonframe,text="STOPSONG",command=self.stopsong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=3,padx=10,pady=5)
# Creating Playlist Frame
songsframe = LabelFrame(self.root,text="Song Playlist",font=("times new roman",15,"bold"),bg="grey",fg="white",bd=5,relief=GROOVE)
songsframe.place(x=600,y=0,width=400,height=200)
# Inserting scrollbar
scrol_y = Scrollbar(songsframe,orient=VERTICAL)
# Inserting Playlist listbox
self.playlist = Listbox(songsframe,yscrollcommand=scrol_y.set,selectbackground="gold",selectmode=SINGLE,font=("times new roman",12,"bold"),bg="silver",fg="navyblue",bd=5,relief=GROOVE)
# Applying Scrollbar to listbox
scrol_y.pack(side=RIGHT,fill=Y)
scrol_y.config(command=self.playlist.yview)
self.playlist.pack(fill=BOTH)
# Changing Directory for fetching Songs
os.chdir("PATH/OF/DIRECTORY")
# Fetching Songs
songtracks = os.listdir()
# Inserting Songs into Playlist
for track in songtracks:
self.playlist.insert(END,track)
After that, we have a tendency to are reaching to turn out the Button Frame and insert play, pause, unpause, and stop buttons into it.
Then can we'll we are going to turn out the playlist frame and add the scrollbar thereto which we are going to add songs into playlist.
The playsong() operate
def playsong(self):
# Displaying Selected Song title
self.track.set(self.playlist.get(ACTIVE))
# Displaying Status
self.status.set("-Playing")
# Loading Selected Song
pygame.mixer.music.load(self.playlist.get(ACTIVE))
# Playing Selected Song
pygame.mixer.music.play()
The stopsong() operate
def stopsong(self):
# Displaying Status
self.status.set("-Stopped")
# Stopped Song
pygame.mixer.music.stop()
The pausesong() operate
def pausesong(self):
# Displaying Status
self.status.set("-Paused")
# Paused Song
pygame.mixer.music.pause()
The unpausesong() operate
def unpausesong(self):
# It will Display the Status
self.status.set("-Playing")
# Playing back Song
pygame.mixer.music.unpause()
Root Window Looping
The command will be:
root.mainloop()
Run this code and enjoy the music
python music player project
python music library
python music bot
python music projects
python music generation
python music player report
python music module
No comments:
Post a Comment