-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicPlayer.py
98 lines (80 loc) · 2.59 KB
/
musicPlayer.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
import os
from tkinter.filedialog import askdirectory
import pygame
from tkinter import *
root = Tk()
root.title("Music Player")
root.geometry('650x450+150+150')
root.configure(bg = "light blue")
frame = Frame(root,width=600, height=900 ,background='light pink').pack()
label = Label(frame, text='Music player').place(x=300,y= 10)
listOfSong = []
pauseSong = True
index = 0
v = StringVar()
songname = ''
songlabel = Label(frame,textvariable=v, width = 50,bg = "light pink").place(x=140,y=380)
# < -------------------------------function------------------------------->
def select_song(event):
cs = listbox.curselection()
global index
index = cs[0]
pygame.mixer.music.load(listOfSong[index])
pygame.mixer.music.play()
updatelabel()
def next_song():
global index
try :
index += 1
pygame.mixer.music.load(listOfSong[index])
pygame.mixer.music.play()
updatelabel()
except IndexError as error:
v.set("")
def previous_song():
try :
global index
index -= 1
pygame.mixer.music.load(listOfSong[index])
pygame.mixer.music.play()
updatelabel()
except IndexError as error :
v.set("")
def pause_song():
global pauseSong
if pauseSong:
pygame.mixer.music.pause()
pauseSong = False
else:
pygame.mixer.music.unpause()
pauseSong = True
return songname
def updatelabel():
global index
global songname
v.set(listOfSong[index])
return songname
def directorychooser():
dir = askdirectory()
os.chdir(dir)
for files in os.listdir(dir):
if files.endswith(".mp3"):
listOfSong.append(files)
pygame.mixer.init()
pygame.mixer.music.load(listOfSong[0])
# --------------------****************----------------------
directorychooser()
# -------------------------Listbox----------------------------
listbox = Listbox(frame, width=40, height=20, selectmode=SINGLE)
listOfSong.reverse()
for items in listOfSong:
listbox.insert(0, items)
listOfSong.reverse()
listbox.bind("<Double-1>",select_song)
listbox.place(x=200,y=30)
# _________________________________________Button____________________________________________________________________
nextbutton = Button(frame, text='Next' ,command =next_song).place(x=340,y=410)
previousbutton = Button(frame, text='Previous', command =previous_song).place(x=230, y=410)
pausebutton = Button(frame, text='pause', command=pause_song).place(x=290, y=410)
root.resizable(False, False)
root.mainloop()