0% found this document useful (0 votes)
31 views3 pages

Cod Sursa

This document contains the code for a music player application created with Python Tkinter. It defines functions for playing, pausing, stopping music and changing tracks. It initializes a mixer to control music playback and creates a GUI with widgets like buttons, scales and lists to control the player. The code loads and plays MP3 files from a selected folder and allows adjusting the volume.

Uploaded by

frankiezvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Cod Sursa

This document contains the code for a music player application created with Python Tkinter. It defines functions for playing, pausing, stopping music and changing tracks. It initializes a mixer to control music playback and creates a GUI with widgets like buttons, scales and lists to control the player. The code loads and plays MP3 files from a selected folder and allows adjusting the volume.

Uploaded by

frankiezvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import os

from tkinter import *


from tkinter import filedialog
from pygame import mixer

root = Tk()
root.title("Music Player")
root.geometry("485x700+290+10")
root.configure(background='#333333')
root.resizable(True, True)
mixer.init()

def SetVolume(val):
mixer.music.set_volume(float(val)/100)

volume = Scale(root, from_=0, to=100, orient=HORIZONTAL, bg='#333333', fg='white',


activebackground='grey', length=150, highlightthickness=0,
troughcolor='#333333',
sliderrelief='flat', sliderlength=15, command=SetVolume)
volume.set(50) # Set default volume to 50
volume.place(x=195, y=563)

def AddMusic():
path = filedialog.askdirectory()
if path:
os.chdir(path)
songs = os.listdir(path)

for song in songs:


if song.endswith(".mp3"):
Playlist.insert(END, song)

def SetVolume(val):
mixer.music.set_volume(float(val)/100)

def PlayMusic():
Music_Name = Playlist.get(ACTIVE)
print(Music_Name[0:-4])
mixer.music.load(Playlist.get(ACTIVE))
mixer.music.set_volume(volume.get()/100) # Set volume
mixer.music.play()

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

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

def PlayNext():
current_index = Playlist.curselection()
if current_index:
next_index = current_index[0] + 1
if next_index < Playlist.size():
Playlist.selection_clear(current_index)
Playlist.activate(next_index)
Playlist.selection_set(next_index)
PlayMusic()

def PlayPrev():
current_index = Playlist.curselection()
if current_index:
prev_index = current_index[0] - 1
if prev_index >= 0:
Playlist.selection_clear(current_index)
Playlist.activate(prev_index)
Playlist.selection_set(prev_index)
PlayMusic()

lower_frame = Frame(root, bg="#FFFFFF", width=485, height=180)


lower_frame.place(x=0, y=400)

image_icon = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\logo


png.png")
root.iconphoto(False, image_icon)

frameCnt = 30
frames = [PhotoImage(file='D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\menu.gif',
format='gif -index %i' % (i)) for i in range(frameCnt)]

def update(ind):
frame = frames[ind]
ind += 1
if ind == frameCnt:
ind = 0
label.configure(image=frame)
root.after(40, update, ind)

label = Label(root)
label.place(x=0, y=0)
root.after(0, update, 0)

ButtonPlay = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\


play.png")
Button(root, image=ButtonPlay, bg="#FFFFFF", bd=0, height=60, width=60,
command=PlayMusic).place(x=215, y=487)

ButtonNext = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\


next.png")
Button(root, image=ButtonNext, bg="#FFFFFF", bd=0, height=60, width=60,
command=PlayNext).place(x=385, y=487)

ButtonPrev = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\


prev.png")
Button(root, image=ButtonPrev, bg="#FFFFFF", bd=0, height=60, width=60,
command=PlayPrev).place(x=40, y=487)

ButtonStop = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\


stop.png")
Button(root, image=ButtonStop, bg="#FFFFFF", bd=0, height=60, width=60,
command=mixer.music.stop).place(x=300, y=487)

ButtonPause = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\


pause.png")
Button(root, image=ButtonPause, bg="#FFFFFF", bd=0, height=60, width=60,
command=mixer.music.pause).place(x=130, y=487)

Menu = PhotoImage(file="D:\\AN 4\\SEM2\AMDM - 4 sem 2\\MP3_Proiect\\menu.png")


Label(root, image=Menu).place(x=0, y=580, width=485, height=120)

Frame_Music = Frame(root, bd=2, relief=RIDGE)


Frame_Music.place(x=0, y=585, width=485, height=100)

Button(root, text="Browse Music", width=59, height=1, font=("calibri",12, "bold"),


fg="Black", bg="#FFFFFF",
command=AddMusic).place(x=0, y=550)

Scroll = Scrollbar(Frame_Music)
Playlist = Listbox(Frame_Music, width=100, font=("Times new roman", 10),
bg="#333333", fg="grey",selectbackground="lightblue", cursor="hand2", bd=0,
yscrollcommand=Scroll.set)
Scroll.config(command=Playlist.yview)
Scroll.pack(side=RIGHT, fill=Y)
Playlist.pack(side=RIGHT, fill=BOTH)

root.mainloop()

You might also like