forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
156 lines (115 loc) · 3.34 KB
/
main.py
File metadata and controls
156 lines (115 loc) · 3.34 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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")
# All the frames
song_frame = LabelFrame(
root, text="Current Song", bg="MediumPurple1", width=400, height=80
)
song_frame.place(x=0, y=0)
button_frame = LabelFrame(
root, text="Control Buttons", bg="purple3", width=400, height=120
)
button_frame.place(y=80)
listbox_frame = LabelFrame(root, text="Playlist", bg="purple4")
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="snow3", font=("Times", 10, "bold")
).place(x=5, y=20)
song_lbl = Label(
song_frame, textvariable=current_song, bg="snow3", 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="snow3",
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="snow3",
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="snow3",
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="snow3",
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="snow3",
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="snow", font=("Times", 9), justify=LEFT).pack(
side=BOTTOM, fill=X
)
# Finalizing the GUI
root.update()
root.mainloop()