-
Notifications
You must be signed in to change notification settings - Fork 1
/
MusicPlayer.py
120 lines (77 loc) · 3.46 KB
/
MusicPlayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Importing all the necessary modules
from tkinter import *
from tkinter import filedialog
import pygame.mixer as mixer # pip install pygame
import os
#Initializing the root window and pygame.mixer
# Initializing the mixer
mixer.init()
# Creating the master GUI for python music player
root = Tk()
root.geometry('700x220')
root.title('Sangeet')
root.resizable(0, 0)
# Play, Stop, Load and Pause & Resume functions
def play_song(song_name: StringVar, songs_list: Listbox, status: StringVar):
song_name.set(songs_list.get(ACTIVE))
mixer.music.load(songs_list.get(ACTIVE))
mixer.music.play()
status.set("Song PLAYING")
def stop_song(status: StringVar):
mixer.music.stop()
status.set("Song STOPPED")
def load(listbox):
os.chdir(filedialog.askdirectory(title='Select Folder'))
tracks = os.listdir()
for track in tracks:
listbox.insert(END, track)
def pause_song(status: StringVar):
mixer.music.pause()
status.set("Song PAUSED")
def resume_song(status: StringVar):
mixer.music.unpause()
status.set("Song RESUMED")
button_bg = "SteelBlue1"
button_fg = "white"
frame_bg = "LightSteelBlue1"
# All the frames
song_frame = LabelFrame(root, text='Current Song', bg=frame_bg , width=400, height=80)
song_frame.place(x=0, y=0)
button_frame = LabelFrame(root, text='Control Buttons', bg=frame_bg, width=400, height=120)
button_frame.place(y=80)
listbox_frame = LabelFrame(root, text='Playlist', bg=frame_bg)
listbox_frame.place(x=400, y=0, height=200, width=300)
# All StringVar variables
current_song = StringVar(root, value='Not selected')
song_status = StringVar(root, value='Not Available')
# Playlist ListBox
playlist = Listbox(listbox_frame, font=('Helvetica', 11), selectbackground='snow3')
scroll_bar = Scrollbar(listbox_frame, orient=VERTICAL)
scroll_bar.pack(side=RIGHT, fill=BOTH)
playlist.config(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=playlist.yview)
playlist.pack(fill=BOTH, padx=5, pady=5)
# SongFrame Labels
Label(song_frame, text='CURRENTLY PLAYING:', bg=frame_bg, font=('Times', 10, 'bold')).place(x=5, y=20)
song_lbl = Label(song_frame, textvariable=current_song, bg=button_fg, font=("Times", 12), width=25)
song_lbl.place(x=150, y=20)
# Buttons in the main screen
pause_btn = Button(button_frame, text='Pause', bg=button_fg, font=("Georgia", 13), width=7,
command=lambda: pause_song(song_status))
pause_btn.place(x=15, y=10)
stop_btn = Button(button_frame, text='Stop', bg=button_fg, font=("Georgia", 13), width=7,
command=lambda: stop_song(song_status))
stop_btn.place(x=105, y=10)
play_btn = Button(button_frame, text='Play', bg=button_fg, font=("Georgia", 13), width=7,
command=lambda: play_song(current_song, playlist, song_status))
play_btn.place(x=195, y=10)
resume_btn = Button(button_frame, text='Resume', bg=button_fg, font=("Georgia", 13), width=7,
command=lambda: resume_song(song_status))
resume_btn.place(x=285, y=10)
load_btn = Button(button_frame, text='Load Directory', bg=button_fg, font=("Georgia", 13), width=35, command=lambda: load(playlist))
load_btn.place(x=10, y=55)
# Label at the bottom that displays the state of the music
Label(root, textvariable=song_status, bg=button_fg, font=('Times', 9), justify=LEFT).pack(side=BOTTOM, fill=X)
# Finalizing the GUI
root.update()
root.mainloop()