-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (74 loc) · 3.36 KB
/
main.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
# Importing all the necessary modules
from tkinter import *
from tkinter import filedialog
import pygame.mixer as mixer # pip install pygame
import os
# Initializing the mixer
mixer.init()
# 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='Open a songs directory'))
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")
# Creating the master GUI
root = Tk()
root.geometry('700x220') #
root.title('Music Player project by Ketan Singh')
root.resizable(0, 0)
# All the frames
song_frame = LabelFrame(root, text='Current Song', bg='LightBlue', width=400, height=80)
song_frame.place(x=0, y=0)
button_frame = LabelFrame(root, text='Control Buttons', bg='Turquoise', width=400, height=120)
button_frame.place(y=80)
listbox_frame = LabelFrame(root, text='Playlist', bg='RoyalBlue')
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='Gold')
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='LightBlue', font=('Times', 10, 'bold')).place(x=5, y=20)
song_lbl = Label(song_frame, textvariable=current_song, bg='Goldenrod', 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='Aqua', 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='Aqua', 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='Aqua', 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='Aqua', 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='Aqua', 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='SteelBlue', font=('Times', 9), justify=LEFT).pack(side=BOTTOM, fill=X)
# Finalizing the GUI
root.update()
root.mainloop()