Python winsound标志错误?

时间:2015-03-12 13:16:43

标签: python tkinter

所以我试图制作一个简单的启动/停止音乐播放器。对于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的{​​{1}}找出正确的语法。我尝试了winsound.PlaySound,对于停止按钮,我尝试了winsound.PlaySound(filename, SND_FILENAME)

我得到的错误

winsound.PlaySound(None, SND_PURGE)

有什么问题?

2 个答案:

答案 0 :(得分:3)

错误消息告诉您SND_PURGESND_FILENAME未定义。由于我没有看到您定义或导入它们,因此您可以理解为什么会收到错误。

查看winsound的文档,看起来这些是该模块的属性。因此,解决方案是导入它们,或者使用模块名称作为前缀。

由于您已经导入了模块,因此只需在模板名称前加上前缀:

winsound.PlaySound(soundfile, winsound.SND_FILENAME)
winsound.PlaySound(None, winsound.SND_PURGE)

(注意winsound.前缀为SND_FILENAMESND_PURGE

答案 1 :(得分:0)

尝试将askopenfilenameplay更改为以下内容:

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