The Gaming Hideout
The Gaming Hideout

Reputation: 584

Python winsound flag error?

So I am trying to make a simple start/stop music player. For the winsound.PlaySound command, the syntax is confusing me. I checked the docs and forum and I cannot seem to find the answer. Here's my code, as well as what I'm trying to accomplish.

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()

What I'm Trying to Do

I can't seem to figure out the right syntax for the flag for winsound.PlaySound. I tried winsound.PlaySound(filename, SND_FILENAME), and for the stop button I've tried winsound.PlaySound(None, SND_PURGE).

The Error I Get

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

What is wrong?

Upvotes: 3

Views: 1628

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386382

The error message is telling you that SND_PURGE and SND_FILENAME are not defined. Since I don't see you defining or importing them, it's understandable why you are getting the error.

Looking at the documentation for winsound, it appears these are attributes of that module. So, the solution is to either import them, or prefix them with the module name.

Since you're already importing the module, just prefix them with the module name:

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

(notice the winsound. prefix to SND_FILENAME and SND_PURGE)

Upvotes: 4

Aaron
Aaron

Reputation: 2393

Try change askopenfilename and play to the following:

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)

Additional information, see winsound.PlaySound.

Upvotes: 0

Related Questions