[英]Python winsound flag error?
因此,我正在嘗試制作一個簡單的啟動/停止音樂播放器。 對於winsound.PlaySound
命令,語法令人困惑。 我檢查了文檔和論壇,但似乎找不到答案。 這是我的代碼,以及我要完成的工作。
import Tkinter, Tkconstants, tkFileDialog
import winsound
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}
# define buttons
Tkinter.Button(self, text='Play', command=self.play).pack(**button_opt)
Tkinter.Button(self, text='Stop', command=self.stop).pack(**button_opt)
# 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, soundfile)
def stop(self):
winsound.PlaySound(None, SND_PURGE)
if __name__=='__main__':
root = Tkinter.Tk()
MusicPlayer(root).pack()
root.wm_title('Music Player')
root.mainloop()
我似乎無法找出winsound.PlaySound
flag
的正確語法。 我嘗試了winsound.PlaySound(filename, SND_FILENAME)
,對於停止按鈕,我嘗試了winsound.PlaySound(None, SND_PURGE)
。
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\MusicPlayer.py", line 72, in play
winsound.PlaySound(soundfile, SND_FILENAME)
NameError: global name 'SND_FILENAME' is not defined
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\MusicPlayer.py", line 75, in stop
winsound.PlaySound(None, SND_PURGE)
NameError: global name 'SND_PURGE' is not defined
錯誤消息告訴您SND_PURGE
和SND_FILENAME
。 由於我看不到您定義或導入它們,因此為什么會出現錯誤是可以理解的。
查看winsound的文檔,似乎這些是該模塊的屬性。 因此,解決方案是導入它們,或為它們添加模塊名稱前綴。
由於您已經導入了模塊,因此只需在模塊名稱前加上前綴即可:
winsound.PlaySound(soundfile, winsound.SND_FILENAME)
winsound.PlaySound(None, winsound.SND_PURGE)
(注意, winsound.
前綴來SND_FILENAME
和SND_PURGE
)
嘗試更改askopenfilename
並play
以下內容:
def askopenfilename(self):
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
return filename
print filename
def play(self):
soundfile = self.askopenfilename()
winsound.PlaySound(soundfile, winsound.SND_FILENAME)
有關更多信息,請參見winsound.PlaySound 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.