-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
245 lines (191 loc) · 7.23 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from tkinter import *
import os
import time
import threading
from mutagen.mp3 import MP3
from tkinter import filedialog
import tkinter.messagebox
from tkinter import ttk #to improve the styles of buttons and widgets
from ttkthemes import themed_tk as tk
from pygame import mixer
def show_Details(play_it):
Main_text['text'] = 'Playing.....' + ' ' + os.path.basename(play_it)
Main_text['anchor'] = 'e'
file_ext = os.path.splitext(play_it)
if file_ext[1] == '.mp3':#To handle mp3 files using mutagen
audio_lenth = MP3(play_it)
total_lenth = audio_lenth.info.length
else:#to handle .wav,.ogg music file extensions
a = mixer.Sound(play_it)
total_lenth = a.get_length()
#divmod fucntion is calculation div = total_lenth/60,mod = total_lenth%60
m,s = divmod(total_lenth,60)
m = round(m)
s = round(s)
time_format = '{:02d}:{:02d}'.format(m,s)
Main_lenth['text'] = 'Duration : '+' '+time_format
thread1 = threading.Thread(target=rem_count,args=(total_lenth,))
thread1.start()
def rem_count(total_lenth):
global paused
curr_secs = 0
while curr_secs<=total_lenth and mixer.music.get_busy():
if paused:
continue
else:
m, s = divmod(curr_secs, 60)
m = round(m)
s = round(s)
m1, s1 = divmod(total_lenth, 60)
m1 = round(m1)
s1 = round(s1)
time_format = '{:02d}:{:02d}'.format(m, s)
time_format1 = '{:02d}:{:02d}'.format(m1, s1)
current_lenth['text'] = 'Cuurent Duration : ' + ' ' + time_format
time.sleep(1)
curr_secs+=1
total_lenth-=1
def Play_music():
global paused
if paused:
mixer.music.unpause()
# global paused = FALSE
status_bar['text'] = 'Playing Music.....' + ' ' + os.path.basename(music_file)
status_bar['anchor'] = 'w'
paused = FALSE
else:
try:
Stop_music()#whenever we are switiching from one song to another song then the program will start 2 threads at a time which will lead to display both songs Remaining time so to over come this problem we will first stop the music and then we will satrt a buffer time of 1 sec to overcome this problem
time.sleep(1)
song = play_list.curselection()#for selecting song from play list box
song = int(song[0])
play_it = music_list[song]#music list is a list of paths of all songs we have added
mixer.music.load(play_it)
mixer.music.play()
status_bar['text'] = 'Playing Music.....'+' '+ os.path.basename(play_it)
status_bar['anchor'] = 'w'
show_Details(play_it)
except:
tkinter.messagebox.showerror("Error","File Not Selected")
def Stop_music():
mixer.music.stop()
status_bar['text'] = 'Music Stopped'
status_bar['anchor'] = 'e'
paused = FALSE
def pause_music():
global paused
paused = TRUE
mixer.music.pause()
status_bar['text'] = 'Music Paused...'
status_bar['anchor'] = 'e'
def rewind_music():
Play_music()
status_bar['text'] = 'Music Rewinded...'+' '+os.path.basename(music_file)
status_bar['anchor'] = 'e'
def close_window_fully():
Stop_music()
exit()
def set_vol(val):
vol = float(val)/100
mixer.music.set_volume(vol)
def about_us():
tkinter.messagebox.showinfo('About Rockerz','This Is A Music Player Devloped With Python Tkinter And Pygame By Robin Singh')
def browse_files():
global music_file
music_file = filedialog.askopenfilename()
add_to_listbox(music_file)
music_list =[]
def add_to_listbox(music_file):
file_name = os.path.basename(music_file)
index = 0
play_list.insert(index,file_name)
music_list.insert(index,music_file)
play_list.pack()
index+=1
def delete_btn():
song = play_list.curselection()
song = int(song[0])
play_list.delete(song)
music_list.pop(song)
def mute_music():
global muted
if muted:
mixer.music.set_volume(.7)
vol_button1.configure(image=pic5)
scale1.set(70)
muted = FALSE
else:
mixer.music.set_volume(0)
vol_button1.configure(image = pic4)
scale1.set(0)
muted = TRUE
def close_window_fully1():
Stop_music()
exit()
muted = FALSE
main_window = tk.ThemedTk()
main_window.get_themes()
main_window.set_theme("breeze") #themes : 'arc','radiance','breeze','ubuntu' etc
#creating toolbar
tool_bar = Menu(main_window)
main_window.config(menu = tool_bar)
status_bar= ttk.Label(main_window,text="Welcome To Rockerzz",relief = SUNKEN,anchor = W,font = 'verdana 10 italic')
status_bar.pack(side = BOTTOM,fill = X)
#creating sub menus
sub_menu = Menu(tool_bar,tearoff = 0)#to remove dashed line from menu
tool_bar.add_cascade(label = 'File',menu = sub_menu)
sub_menu.add_command(label = "Open",command = browse_files)
sub_menu.add_command(label = "Exit",command = close_window_fully1)
sub_menu = Menu(tool_bar,tearoff = 0)#to remove dashed line from menu
tool_bar.add_cascade(label = 'Help',menu = sub_menu)
sub_menu.add_command(label = "About Us ",command = about_us)
mixer.init()
# main_window.geometry("600x300")
main_window.title("Rockerz")
main_window.iconbitmap(r"rockerz.ico")
left_frame = Frame(main_window)
left_frame.pack(side = RIGHT,padx = 30,pady = 20)
play_list = Listbox(left_frame)
play_list.pack()
add_btn = ttk.Button(left_frame,text = 'ADD',command = browse_files)
add_btn.pack(side = LEFT,padx = 3)
del_btn = ttk.Button(left_frame,text = 'DELETE',command = delete_btn)
del_btn.pack(side = LEFT)
right_frame = Frame(main_window)
right_frame.pack(pady = 20)
r_top_frame = Frame(right_frame)
r_top_frame.pack()
Main_text = ttk.Label(r_top_frame,text = "Devloped By Robin Singh",font = 'arial 10 italic')
Main_text.pack()
Main_lenth = ttk.Label(r_top_frame,text = "Length : --:--",relief = GROOVE)
Main_lenth.pack(pady = 5)
current_lenth = ttk.Label(r_top_frame,text = "Current Duration : --:--",relief = GROOVE)
current_lenth.pack()
playlist_box = Listbox(main_window)
canvas = Frame(right_frame)
canvas.pack(pady = 5)
pic = PhotoImage(file = "play.png")
play_button1 = ttk.Button(canvas,image = pic,command = Play_music)
play_button1.grid(row =0 , column = 0,padx=5)
pic1 = PhotoImage(file = "stop.png")
stop_button1 = ttk.Button(canvas,image = pic1,command = Stop_music)
stop_button1.grid(row =0 , column = 1,padx=5)
pic2 = PhotoImage(file = "pause.png")
pause_button1 = ttk.Button(canvas,image = pic2,command = pause_music)
pause_button1.grid(row =0 , column = 2,padx=5)
bottom_canvas = Frame(right_frame)
bottom_canvas.pack(padx= 30,pady=30)
pic3 = PhotoImage(file = "rewind.png")
rewind_button1 = ttk.Button(bottom_canvas,image = pic3,command = rewind_music)
rewind_button1.grid(row =0 , column = 0,pady = 10)
pic4 = PhotoImage(file = "002-mute.png")
pic5 = PhotoImage(file = "001-volume.png")
vol_button1 = ttk.Button(bottom_canvas,image = pic5,command = mute_music)
vol_button1.grid(row =0 , column = 1)
scale1 = ttk.Scale(bottom_canvas,from_ = 0,to = 100,orient = HORIZONTAL,command = set_vol)
scale1.set(50)
mixer.music.set_volume(.5)
scale1.grid(row =0 , column = 3,padx=5,pady =10)
#For overriding close button
main_window.protocol("WM_DELETE_WINDOW",close_window_fully)
main_window.mainloop()