0
我怎樣才能使用winsound.PlaySound()
函數來播放存儲的變量而不是特定的聲音?我正在製作一個臨時音樂播放器,我希望能夠簡單地播放用戶選擇的歌曲。這是我目前的代碼。WinSound從變量播放聲音?
import Tkinter, Tkconstants, tkFileDialog
import winsound
from Tkconstants import *
from tkFileDialog import *
from PIL import Image, ImageTk
class MusicPlayer(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
# options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
#options for images
# define image
img = Image.open('musicPlayer.PNG')
bg = ImageTk.PhotoImage(img)
label = Tkinter.Label(image=bg)
label.image = bg
label.pack()
# define buttons
but1 = Tkinter.Button(self, text='Play', command=self.play)
but2 = Tkinter.Button(self, text='Stop', command=self.stop)
but1.grid(sticky="S, W, E", column=1)
but1.grid(sticky="S, W, E", column=1)
# define options for opening or saving a file
self.file_opt = options = {}
options['defaultextension'] = '*.wav'
options['filetypes'] = [('WAV Sound Files', '*.wav')]
options['initialdir'] = 'C:\\'
options['initialfile'] = '.wav'
options['parent'] = root
options['title'] = 'Pick a File'
# This is only available on the Macintosh, and only when Navigation Services are installed.
#options['message'] = 'message'
# if you use the multiple file version of the module functions this option is set automatically.
#options['multiple'] = 1
# defining options for opening a directory
self.dir_opt = options = {}
options['initialdir'] = 'C:\\'
options['mustexist'] = False
options['parent'] = root
options['title'] = 'Pick a Dir'
def askopenfile(self):
return tkFileDialog.askopenfile(mode='r', **self.file_opt)
def askopenfilename(self):
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
return open(filename, 'r')
print filename
def asksaveasfile(self):
return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
def asksaveasfilename(self):
# get filename
filename = tkFileDialog.asksaveasfilename(**self.file_opt)
# open file on your own
if filename:
return open(filename, 'w')
def askdirectory(self):
return tkFileDialog.askdirectory(**self.dir_opt)
def play(self):
soundfile = self.askopenfilename()
winsound.PlaySound(soundfile, winsound.SND_FILENAME)
def stop(self):
winsound.PlaySound(None, winsound.SND_PURGE)
if __name__=='__main__':
root = Tkinter.Tk()
root.iconbitmap(r'C:\Python27\DLLs\musicPlayer.ico')
MusicPlayer(root).pack()
root.wm_title('Music Player')
root.mainloop()
要注意的事情
- 我是新來的廣州市運生插件,以及Python本身,我不知道這標誌我應該使用。
錯誤,我得到
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "C:\Users\Brenneman.Josh.19\Downloads\code (2)\MusicPlayer.py", line 88, in play
winsound.PlaySound(soundfile, winsound.SND_FILENAME)
TypeError: must be string or read-only buffer, not file
你要完成什麼改變?你想讀取每首歌曲文件並將其存儲在程序中的一個變量中,而不是簡單地按名稱獲取每個文件來播放它?我沒有看到你要求一個文件名,然後將該字符串傳遞給PlaySound()的方式有什麼問題。 – TigerhawkT3 2015-03-13 19:36:11
@ TigerhawkT3它給了我一個關於它不是一個有效的路徑或字符串或類似的錯誤。我將用錯誤編輯帖子。 – 2015-03-23 15:17:22
您定義了自己的'askopenfilename',它返回與您定義的'askopenfile'相同的東西:一個文件對象。正如回溯說的那樣,'winsound.PlaySound()'需要一個帶有聲音文件路徑的字符串,例如''聲音/ mysound.wav',而不是一個打開的文件對象。只需直接使用'tkFileDialog'函數,而不是將它們包裝到自己的函數中。你的asksaveasfilename函數可以工作嗎? – TigerhawkT3 2015-03-23 18:36:07