因此,我正在尝试制作一个简单的启动/停止音乐播放器。对于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()
我似乎无法找出正确的语法,flag
对winsound.PlaySound
。我尝试了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
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句