|
| 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 | + |
| 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() |
| 68 | + time.sleep(1) |
| 69 | + song = play_list.curselection() |
| 70 | + song = int(song[0]) |
| 71 | + play_it = music_list[song] |
| 72 | + |
| 73 | + mixer.music.load(play_it) |
| 74 | + mixer.music.play() |
| 75 | + status_bar['text'] = 'Playing Music.....' + \ |
| 76 | + ' ' + os.path.basename(play_it) |
| 77 | + status_bar['anchor'] = 'w' |
| 78 | + show_Details(play_it) |
| 79 | + except: |
| 80 | + tkinter.messagebox.showerror("Error", "File Not Selected") |
| 81 | + |
| 82 | + |
| 83 | +def Stop_music(): |
| 84 | + mixer.music.stop() |
| 85 | + status_bar['text'] = 'Music Stopped' |
| 86 | + status_bar['anchor'] = 'e' |
| 87 | + |
| 88 | + |
| 89 | +paused = FALSE |
| 90 | + |
| 91 | + |
| 92 | +def pause_music(): |
| 93 | + global paused |
| 94 | + paused = TRUE |
| 95 | + mixer.music.pause() |
| 96 | + status_bar['text'] = 'Music Paused...' |
| 97 | + status_bar['anchor'] = 'e' |
| 98 | + |
| 99 | + |
| 100 | +def rewind_music(): |
| 101 | + Play_music() |
| 102 | + status_bar['text'] = 'Music Rewinded...'+' '+os.path.basename(music_file) |
| 103 | + status_bar['anchor'] = 'e' |
| 104 | + |
| 105 | + |
| 106 | +def close_window_fully(): |
| 107 | + Stop_music() |
| 108 | + exit() |
| 109 | + |
| 110 | + |
| 111 | +def set_vol(val): |
| 112 | + vol = float(val)/100 |
| 113 | + mixer.music.set_volume(vol) |
| 114 | + |
| 115 | + |
| 116 | +def about_us(): |
| 117 | + tkinter.messagebox.showinfo( |
| 118 | + 'About Rockerz', 'This Is A Music Player Devloped With Python Tkinter And Pygame By Robin Singh') |
| 119 | + |
| 120 | + |
| 121 | +def browse_files(): |
| 122 | + global music_file |
| 123 | + music_file = filedialog.askopenfilename() |
| 124 | + add_to_listbox(music_file) |
| 125 | + |
| 126 | + |
| 127 | +music_list = [] |
| 128 | + |
| 129 | + |
| 130 | +def add_to_listbox(music_file): |
| 131 | + file_name = os.path.basename(music_file) |
| 132 | + index = 0 |
| 133 | + play_list.insert(index, file_name) |
| 134 | + music_list.insert(index, music_file) |
| 135 | + play_list.pack() |
| 136 | + index += 1 |
| 137 | + |
| 138 | + |
| 139 | +def delete_btn(): |
| 140 | + song = play_list.curselection() |
| 141 | + song = int(song[0]) |
| 142 | + play_list.delete(song) |
| 143 | + music_list.pop(song) |
| 144 | + |
| 145 | + |
| 146 | +def mute_music(): |
| 147 | + global muted |
| 148 | + if muted: |
| 149 | + mixer.music.set_volume(.7) |
| 150 | + vol_button1.configure(image=pic5) |
| 151 | + scale1.set(70) |
| 152 | + muted = FALSE |
| 153 | + |
| 154 | + else: |
| 155 | + mixer.music.set_volume(0) |
| 156 | + vol_button1.configure(image=pic4) |
| 157 | + scale1.set(0) |
| 158 | + muted = TRUE |
| 159 | + |
| 160 | + |
| 161 | +def close_window_fully1(): |
| 162 | + Stop_music() |
| 163 | + exit() |
| 164 | + |
| 165 | + |
| 166 | +muted = FALSE |
| 167 | + |
| 168 | + |
| 169 | +main_window = tk.ThemedTk() |
| 170 | +main_window.get_themes() |
| 171 | +# themes : 'arc','radiance','breeze','ubuntu' etc |
| 172 | +main_window.set_theme("breeze") |
| 173 | +# creating toolbar |
| 174 | +tool_bar = Menu(main_window) |
| 175 | +main_window.config(menu=tool_bar) |
| 176 | + |
| 177 | +status_bar = ttk.Label(main_window, text="Welcome To Rockerzz", |
| 178 | + relief=SUNKEN, anchor=W, font='verdana 10 italic') |
| 179 | +status_bar.pack(side=BOTTOM, fill=X) |
| 180 | + |
| 181 | +# creating sub menus |
| 182 | +sub_menu = Menu(tool_bar, tearoff=0) # to remove dashed line from menu |
| 183 | +tool_bar.add_cascade(label='File', menu=sub_menu) |
| 184 | +sub_menu.add_command(label="Open", command=browse_files) |
| 185 | +sub_menu.add_command(label="Exit", command=close_window_fully1) |
| 186 | + |
| 187 | +sub_menu = Menu(tool_bar, tearoff=0) # to remove dashed line from menu |
| 188 | +tool_bar.add_cascade(label='Help', menu=sub_menu) |
| 189 | +sub_menu.add_command(label="About Us ", command=about_us) |
| 190 | + |
| 191 | + |
| 192 | +mixer.init() |
| 193 | + |
| 194 | +# main_window.geometry("600x300") |
| 195 | +main_window.title("Rockerz") |
| 196 | +main_window.iconbitmap("./Music-Player-App/assests/rockerz.ico") |
| 197 | + |
| 198 | +left_frame = Frame(main_window) |
| 199 | +left_frame.pack(side=RIGHT, padx=30, pady=20) |
| 200 | +play_list = Listbox(left_frame) |
| 201 | +play_list.pack() |
| 202 | + |
| 203 | + |
| 204 | +add_btn = ttk.Button(left_frame, text='ADD', command=browse_files) |
| 205 | +add_btn.pack(side=LEFT, padx=3) |
| 206 | + |
| 207 | +del_btn = ttk.Button(left_frame, text='DELETE', command=delete_btn) |
| 208 | +del_btn.pack(side=LEFT) |
| 209 | + |
| 210 | +right_frame = Frame(main_window) |
| 211 | +right_frame.pack(pady=20) |
| 212 | + |
| 213 | +r_top_frame = Frame(right_frame) |
| 214 | +r_top_frame.pack() |
| 215 | + |
| 216 | +Main_text = ttk.Label( |
| 217 | + r_top_frame, text="Devloped By Robin Singh", font='arial 10 italic') |
| 218 | +Main_text.pack() |
| 219 | + |
| 220 | +Main_lenth = ttk.Label(r_top_frame, text="Length : --:--", relief=GROOVE) |
| 221 | +Main_lenth.pack(pady=5) |
| 222 | + |
| 223 | +current_lenth = ttk.Label( |
| 224 | + r_top_frame, text="Current Duration : --:--", relief=GROOVE) |
| 225 | +current_lenth.pack() |
| 226 | + |
| 227 | +playlist_box = Listbox(main_window) |
| 228 | +canvas = Frame(right_frame) |
| 229 | +canvas.pack(pady=5) |
| 230 | + |
| 231 | +pic = PhotoImage(file="./Music-Player-App/assests/images/play.png") |
| 232 | +play_button1 = ttk.Button(canvas, image=pic, command=Play_music) |
| 233 | +play_button1.grid(row=0, column=0, padx=5) |
| 234 | + |
| 235 | +pic1 = PhotoImage(file="./Music-Player-App/assests/images/stop.png") |
| 236 | +stop_button1 = ttk.Button(canvas, image=pic1, command=Stop_music) |
| 237 | +stop_button1.grid(row=0, column=1, padx=5) |
| 238 | + |
| 239 | +pic2 = PhotoImage(file="./Music-Player-App/assests/images/pause.png") |
| 240 | +pause_button1 = ttk.Button(canvas, image=pic2, command=pause_music) |
| 241 | +pause_button1.grid(row=0, column=2, padx=5) |
| 242 | + |
| 243 | +bottom_canvas = Frame(right_frame) |
| 244 | +bottom_canvas.pack(padx=30, pady=30) |
| 245 | +pic3 = PhotoImage(file="./Music-Player-App/assests/images/rewind.png") |
| 246 | +rewind_button1 = ttk.Button(bottom_canvas, image=pic3, command=rewind_music) |
| 247 | +rewind_button1.grid(row=0, column=0, pady=10) |
| 248 | + |
| 249 | +pic4 = PhotoImage(file="./Music-Player-App/assests/images/002-mute.png") |
| 250 | +pic5 = PhotoImage(file="./Music-Player-App/assests/images/001-volume.png") |
| 251 | +vol_button1 = ttk.Button(bottom_canvas, image=pic5, command=mute_music) |
| 252 | +vol_button1.grid(row=0, column=1) |
| 253 | + |
| 254 | + |
| 255 | +scale1 = ttk.Scale(bottom_canvas, from_=0, to=100, |
| 256 | + orient=HORIZONTAL, command=set_vol) |
| 257 | +scale1.set(50) |
| 258 | +mixer.music.set_volume(.5) |
| 259 | +scale1.grid(row=0, column=3, padx=5, pady=10) |
| 260 | + |
| 261 | +# For overriding close button |
| 262 | +main_window.protocol("WM_DELETE_WINDOW", close_window_fully) |
| 263 | +main_window.mainloop() |
0 commit comments