Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog as fd
- import os
- import sys
- import winsound
- import math
- import time
- from threading import Thread
- global sf
- try:
- import soundfile as sf
- except ModuleNotFoundError:
- os.system('pip install soundfile')
- import soundfile as sf
- musicfolderpath = 'T:/Storage/Music'
- musicfolderall = os.listdir(musicfolderpath)
- musicfolder = [file for file in musicfolderall if file.endswith(".wav")]
- def pause(secs):
- global time
- init_time = time.time()
- while time.time() < init_time+secs: pass
- def gettime(secs):
- apsecs = round(secs)
- minutes = apsecs / 60
- apminutes = math.floor(minutes)
- minutesecs = apminutes * 60
- exsecs = apsecs - minutesecs
- if len(str(exsecs)) == 1:
- exsecs = "0" + str(exsecs)
- else:
- pass
- time = str(apminutes) + ":" + str(exsecs)
- return time
- def updatemusicfolder():
- global musicfolder
- try:
- musicfolderall = os.listdir(musicfolderpath)
- musicfolder = [file for file in musicfolderall if file.endswith(".wav")]
- except:
- pass
- updatemusicfolder()
- def changepath():
- global musicfolderpath
- global updatemusicfolder
- global checkmusic
- settingsw.destroy()
- newpath = fd.askdirectory(title='Select Music Folder')
- if not newpath == '':
- musicfolderpath = newpath
- updatemusicfolder()
- checkmusic()
- else:
- pass
- _root = frame1
- currentlyplaying = tk.StringVar()
- currentlyplaying.set("Now Playing: Nothing currently playing")
- musicplaying = False
- def playmusic(file):
- global playmusicthread
- global Thread
- Thread(target=lambda: playmusicthread(file), daemon=True).start()
- def playmusicthread(file):
- global currentlyplaying
- global musicplaying
- musicplaying = False
- global musicfolderpath
- global winsound
- path = os.path.join(musicfolderpath, file)
- filename = file[:-4]
- if len(filename) > 25:
- cutfilename = filename[:22] + '...'
- else:
- cutfilename = filename
- winsound.PlaySound(path, winsound.SND_ASYNC)
- soundf = sf.SoundFile(path)
- seconds = len(soundf) / soundf.samplerate
- apsecs = round(seconds)
- fulltime = gettime(apsecs)
- playedsecs = 0
- currentlyplaying.set("Now Playing: " + cutfilename + " (" + gettime(playedsecs) + " / " + fulltime + ")")
- musicplaying = True
- while musicplaying == True and not playedsecs == apsecs:
- pause(1)
- playedsecs += 1
- currentlyplaying.set("Now Playing: " + cutfilename + " (" + gettime(playedsecs) + " / " + fulltime + ")")
- musicplaying = False
- playingall = False
- def playallthread():
- global musicfolder
- global musicplaying
- global playingall
- global pause
- playingall = True
- for element in musicfolder:
- pause(1)
- if playingall == True:
- playmusic(element)
- while True:
- if musicplaying == True:
- while musicplaying == True:
- pass
- break
- else:
- pass
- else:
- break
- def playall():
- global playallthread
- global thread
- Thread(target=playallthread, daemon=True).start()
- def stopmusic():
- global musicplaying
- global playingall
- global winsound
- playingall = False
- musicplaying = False
- winsound.PlaySound(None, winsound.SND_PURGE)
- container = tk.Frame(_root, bd=0, bg='white')
- canvas = tk.Canvas(container, bg='white', bd=0, highlightthickness=0)
- scrollbar = tk.Scrollbar(container, orient="vertical", command=canvas.yview, troughcolor='white', width=12)
- view = 'compact' # other options are normal and large
- def changeview():
- global view
- if view == 'compact':
- view = 'normal'
- elif view == 'normal':
- view = 'large'
- elif view == 'large':
- view = 'compact'
- checkmusic()
- def settings():
- global view
- global changepath
- global fd
- global changeview
- global checkmusic
- global settingsw
- global tk
- settingsw = tk.Toplevel(root)
- settingsw.minsize(400, 300)
- settingsw.resizable(False, False)
- settingsw.title('Settings')
- settingsw.configure(bg='white')
- settingslabel = tk.Label(settingsw, text='Settings', bg='white', font='arial 20')
- settingslabel.pack(side='top', anchor='nw', padx=10, pady=10)
- changepathbutton = tk.Button(settingsw, text='Change music folder', command=changepath, bd=1, relief='solid', bg='white')
- changepathbutton.pack(side='top', anchor='nw', padx=10, pady=10)
- currentview = tk.StringVar()
- if view == 'compact':
- currentview.set('View: Compact')
- elif view == 'normal':
- currentview.set('View: Normal')
- elif view == 'large':
- currentview.set('View: Large')
- def cycleview():
- changeview()
- if view == 'compact':
- currentview.set('View: Compact')
- elif view == 'normal':
- currentview.set('View: Normal')
- elif view == 'large':
- currentview.set('View: Large')
- cycleviewbutton = tk.Button(settingsw, textvar=currentview, command=cycleview, bd=1, relief='solid', bg='white')
- cycleviewbutton.pack(side='top', anchor='nw', padx=10, pady=10)
- def checkmusic():
- global musicframe
- global musicfolder
- global canvas
- global scrollbar
- global musicfolderpath
- global view
- global gettime
- global math
- global playmusic
- global tk
- global os
- try:
- musicframe.destroy()
- except:
- pass
- musicframe = tk.Frame(canvas, bd=0, bg='white', highlightcolor='white', highlightthickness=0)
- musicframe.bind(
- "<Configure>",
- lambda e: canvas.configure(
- scrollregion=canvas.bbox("all")
- )
- )
- canvas.create_window((0, 0), window=musicframe, anchor="nw")
- canvas.configure(yscrollcommand=scrollbar.set)
- if not musicfolder == []:
- for element in musicfolder:
- path = os.path.join(musicfolderpath, element)
- soundf = sf.SoundFile(path)
- seconds = len(soundf) / soundf.samplerate
- if view == 'compact':
- songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + " (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, command=lambda element=element: playmusic(element))
- elif view == 'normal':
- songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + " (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, pady=3, command=lambda element=element: playmusic(element))
- elif view == 'large':
- songbutton = tk.Button(musicframe, bd=0, text="\u25b6 " + element[:-4] + " (" + gettime(seconds) + ")", wraplength=585, justify='left', bg='white', anchor='w', padx=9, pady=6, command=lambda element=element: playmusic(element))
- songbutton.pack(anchor='w', expand=True, fill='x')
- else:
- nomusiclabel = tk.Button(musicframe, text="Hmmm. It seems like there isn't any music in this folder. Click here to change your music folder (Keep in mind, we only support wav formats.)", wraplength=585, bg='white', command=changepath, bd=0)
- nomusiclabel.pack(anchor='center', expand=True, fill='x')
- scrollbar.update()
- container.pack(expand=True, fill='both')
- canvas.place(x=0, y=0, relwidth=1.0, relheight=1.0)
- scrollbar.pack(side="right", fill="y")
- stopbutton = tk.Button(container, text='\u25a0', command=stopmusic, bd=1, relief='solid', bg='white', padx=5)
- stopbutton.pack(side='right', anchor='se', padx=5, pady=5)
- playallbutton = tk.Button(container, text='\u25b6 All', command=playall, bd=1, relief='solid', bg='white', padx=5)
- playallbutton.pack(side='right', anchor='se', padx=5, pady=5)
- refreshbutton = tk.Button(container, text='\u27f3', command=checkmusic, bd=1, relief='solid', bg='white', padx=5)
- refreshbutton.pack(side='right', anchor='se', padx=5, pady=5)
- settingsbutton = tk.Button(container, text='Settings', command=settings, bd=1, relief='solid', bg='white')
- settingsbutton.pack(side='right', anchor='se', padx=5, pady=5)
- currentlyplayinglabel = tk.Label(container, textvar=currentlyplaying, bg='white', justify='right', bd=1, relief='solid', padx=4, pady=3)
- currentlyplayinglabel.pack(side='bottom', anchor='se', padx=5, pady=5)
- checkmusic()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement