-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicplayer.py
62 lines (51 loc) · 2.11 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
from Tkinter import *
import Tkinter.filedialog as tk
import Tkinter.messagebox as tk2
import pygame
playlist = []
class Application(Frame):
def __init__(self,master):
super(Application, self).__init__(master)
#self.create_widgets()
self.playlistbox = Listbox(self, width = 40, height = 10, selectmode = SINGLE) #TODO: ---> BROWSE, MULTIPLE, EXTENDED (p.379)
for song in playlist:
self.playlistbox.insert(END, song)
self.grid(rowspan=5, columnspan=4)
self.playlistbox.grid(row = 1)
self.playButton = Button(self, text = 'Play', command = self.play)
self.loopButton = Button(self, text = 'Loop', command = self.loop)
self.addButton = Button(self, text = 'Add', command = self.add)
self.playButton.grid(row=4, column = 0)
self.loopButton.grid(row=4, column = 1)
self.addButton.grid(row=4, column = 2)
self.pack()
#pygame initialize
pygame.init()
def play(self):
if(len(playlist) == 0):
tk2.showinfo('Notice', 'No songs in your playlist!\nClick Add to add songs.')
else:
pygame.mixer.music.stop()
selectedSongs = self.playlistbox.curselection()
global playlistbox
playIt = playlist[int(selectedSongs[0])]
pygame.mixer.music.load(playIt)
pygame.mixer.music.play(0, 0.0)
def loop(self):
pygame.mixer.music.stop()
pygame.mixer.music.play(-1,0.0)
def add(self):
file = tk.askopenfilenames(initialdir='C:/Users/babbu/Downloads')
songsTuple = root.splitlist(file) #turn user's opened filenames into tuple
songsList = list(songsTuple) #convert to list
#Add the full filename of songto playlist list, and a shortened version to the listBox
for song in songsList:
playlist.append(song);
tempArray = song.split('/')
songShort = tempArray[len(tempArray)-1]
self.playlistbox.insert(END, songShort)
root = Tk()
root.title('Text Editor')
root.geometry('500x200')
app = Application(root)
app.mainloop()