Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 458da9e

Browse files
main program
1 parent a204a6c commit 458da9e

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed

‎Music-Player-App/main.py‎

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
from tkinter import *
2+
import os
3+
import time
4+
import threading
5+
from mutagen.mp3 import MP3
6+
from tkinter import filedialog
7+
import tkinter.messagebox
8+
from tkinter import ttk # to improve the styles of buttons and widgets
9+
from ttkthemes import themed_tk as tk
10+
from pygame import mixer
11+
12+
13+
def show_Details(play_it):
14+
Main_text['text'] = 'Playing.....' + ' ' + os.path.basename(play_it)
15+
Main_text['anchor'] = 'e'
16+
file_ext = os.path.splitext(play_it)
17+
if file_ext[1] == '.mp3': # To handle mp3 files using mutagen
18+
audio_lenth = MP3(play_it)
19+
total_lenth = audio_lenth.info.length
20+
else: # to handle .wav,.ogg music file extensions
21+
a = mixer.Sound(play_it)
22+
total_lenth = a.get_length()
23+
# divmod fucntion is calculation div = total_lenth/60,mod = total_lenth%60
24+
m, s = divmod(total_lenth, 60)
25+
m = round(m)
26+
s = round(s)
27+
28+
time_format = '{:02d}:{:02d}'.format(m, s)
29+
Main_lenth['text'] = 'Duration : '+' '+time_format
30+
thread1 = threading.Thread(target=rem_count, args=(total_lenth,))
31+
thread1.start()
32+
33+
34+
def rem_count(total_lenth):
35+
global paused
36+
curr_secs = 0
37+
while curr_secs <= total_lenth and mixer.music.get_busy():
38+
if paused:
39+
continue
40+
else:
41+
m, s = divmod(curr_secs, 60)
42+
m = round(m)
43+
s = round(s)
44+
m1, s1 = divmod(total_lenth, 60)
45+
m1 = round(m1)
46+
s1 = round(s1)
47+
48+
time_format = '{:02d}:{:02d}'.format(m, s)
49+
time_format1 = '{:02d}:{:02d}'.format(m1, s1)
50+
current_lenth['text'] = 'Cuurent Duration : ' + ' ' + time_format
51+
time.sleep(1)
52+
curr_secs += 1
53+
total_lenth -= 1
54+
55+
56+
def Play_music():
57+
global paused
58+
if paused:
59+
mixer.music.unpause()
60+
# global paused = FALSE
61+
status_bar['text'] = 'Playing Music.....' + \
62+
' ' + os.path.basename(music_file)
63+
status_bar['anchor'] = 'w'
64+
paused = FALSE
65+
else:
66+
try:
67+
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
68+
time.sleep(1)
69+
song = play_list.curselection() # for selecting song from play list box
70+
song = int(song[0])
71+
# music list is a list of paths of all songs we have added
72+
play_it = music_list[song]
73+
74+
mixer.music.load(play_it)
75+
mixer.music.play()
76+
status_bar['text'] = 'Playing Music.....' + \
77+
' ' + os.path.basename(play_it)
78+
status_bar['anchor'] = 'w'
79+
show_Details(play_it)
80+
except:
81+
tkinter.messagebox.showerror("Error", "File Not Selected")
82+
83+
84+
def Stop_music():
85+
mixer.music.stop()
86+
status_bar['text'] = 'Music Stopped'
87+
status_bar['anchor'] = 'e'
88+
89+
90+
paused = FALSE
91+
92+
93+
def pause_music():
94+
global paused
95+
paused = TRUE
96+
mixer.music.pause()
97+
status_bar['text'] = 'Music Paused...'
98+
status_bar['anchor'] = 'e'
99+
100+
101+
def rewind_music():
102+
Play_music()
103+
status_bar['text'] = 'Music Rewinded...'+' '+os.path.basename(music_file)
104+
status_bar['anchor'] = 'e'
105+
106+
107+
def close_window_fully():
108+
Stop_music()
109+
exit()
110+
111+
112+
def set_vol(val):
113+
vol = float(val)/100
114+
mixer.music.set_volume(vol)
115+
116+
117+
def about_us():
118+
tkinter.messagebox.showinfo(
119+
'About Rockerz', 'This Is A Music Player Devloped With Python Tkinter And Pygame By Robin Singh')
120+
121+
122+
def browse_files():
123+
global music_file
124+
music_file = filedialog.askopenfilename()
125+
add_to_listbox(music_file)
126+
127+
128+
music_list = []
129+
130+
131+
def add_to_listbox(music_file):
132+
file_name = os.path.basename(music_file)
133+
index = 0
134+
play_list.insert(index, file_name)
135+
music_list.insert(index, music_file)
136+
play_list.pack()
137+
index += 1
138+
139+
140+
def delete_btn():
141+
song = play_list.curselection()
142+
song = int(song[0])
143+
play_list.delete(song)
144+
music_list.pop(song)
145+
146+
147+
def mute_music():
148+
global muted
149+
if muted:
150+
mixer.music.set_volume(.7)
151+
vol_button1.configure(image=pic5)
152+
scale1.set(70)
153+
muted = FALSE
154+
155+
else:
156+
mixer.music.set_volume(0)
157+
vol_button1.configure(image=pic4)
158+
scale1.set(0)
159+
muted = TRUE
160+
161+
162+
def close_window_fully1():
163+
Stop_music()
164+
exit()
165+
166+
167+
muted = FALSE
168+
169+
170+
main_window = tk.ThemedTk()
171+
main_window.get_themes()
172+
# themes : 'arc','radiance','breeze','ubuntu' etc
173+
main_window.set_theme("breeze")
174+
# creating toolbar
175+
tool_bar = Menu(main_window)
176+
main_window.config(menu=tool_bar)
177+
178+
status_bar = ttk.Label(main_window, text="Welcome To Rockerzz",
179+
relief=SUNKEN, anchor=W, font='verdana 10 italic')
180+
status_bar.pack(side=BOTTOM, fill=X)
181+
182+
# creating sub menus
183+
sub_menu = Menu(tool_bar, tearoff=0) # to remove dashed line from menu
184+
tool_bar.add_cascade(label='File', menu=sub_menu)
185+
sub_menu.add_command(label="Open", command=browse_files)
186+
sub_menu.add_command(label="Exit", command=close_window_fully1)
187+
188+
sub_menu = Menu(tool_bar, tearoff=0) # to remove dashed line from menu
189+
tool_bar.add_cascade(label='Help', menu=sub_menu)
190+
sub_menu.add_command(label="About Us ", command=about_us)
191+
192+
193+
mixer.init()
194+
195+
# main_window.geometry("600x300")
196+
main_window.title("Rockerz")
197+
198+
199+
left_frame = Frame(main_window)
200+
left_frame.pack(side=RIGHT, padx=30, pady=20)
201+
play_list = Listbox(left_frame)
202+
play_list.pack()
203+
204+
205+
add_btn = ttk.Button(left_frame, text='ADD', command=browse_files)
206+
add_btn.pack(side=LEFT, padx=3)
207+
208+
del_btn = ttk.Button(left_frame, text='DELETE', command=delete_btn)
209+
del_btn.pack(side=LEFT)
210+
211+
right_frame = Frame(main_window)
212+
right_frame.pack(pady=20)
213+
214+
r_top_frame = Frame(right_frame)
215+
r_top_frame.pack()
216+
217+
Main_text = ttk.Label(
218+
r_top_frame, text="Devloped By Robin Singh", font='arial 10 italic')
219+
Main_text.pack()
220+
221+
Main_lenth = ttk.Label(r_top_frame, text="Length : --:--", relief=GROOVE)
222+
Main_lenth.pack(pady=5)
223+
224+
current_lenth = ttk.Label(
225+
r_top_frame, text="Current Duration : --:--", relief=GROOVE)
226+
current_lenth.pack()
227+
228+
playlist_box = Listbox(main_window)
229+
canvas = Frame(right_frame)
230+
canvas.pack(pady=5)
231+
232+
pic = PhotoImage(file="play.png")
233+
play_button1 = ttk.Button(canvas, image=pic, command=Play_music)
234+
play_button1.grid(row=0, column=0, padx=5)
235+
236+
pic1 = PhotoImage(file="stop.png")
237+
stop_button1 = ttk.Button(canvas, image=pic1, command=Stop_music)
238+
stop_button1.grid(row=0, column=1, padx=5)
239+
240+
pic2 = PhotoImage(file="pause.png")
241+
pause_button1 = ttk.Button(canvas, image=pic2, command=pause_music)
242+
pause_button1.grid(row=0, column=2, padx=5)
243+
244+
bottom_canvas = Frame(right_frame)
245+
bottom_canvas.pack(padx=30, pady=30)
246+
pic3 = PhotoImage(file="rewind.png")
247+
rewind_button1 = ttk.Button(bottom_canvas, image=pic3, command=rewind_music)
248+
rewind_button1.grid(row=0, column=0, pady=10)
249+
250+
pic4 = PhotoImage(file="002-mute.png")
251+
pic5 = PhotoImage(file="001-volume.png")
252+
vol_button1 = ttk.Button(bottom_canvas, image=pic5, command=mute_music)
253+
vol_button1.grid(row=0, column=1)
254+
255+
256+
scale1 = ttk.Scale(bottom_canvas, from_=0, to=100,
257+
orient=HORIZONTAL, command=set_vol)
258+
scale1.set(50)
259+
mixer.music.set_volume(.5)
260+
scale1.grid(row=0, column=3, padx=5, pady=10)
261+
262+
# For overriding close button
263+
main_window.protocol("WM_DELETE_WINDOW", close_window_fully)
264+
main_window.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /