0

I have the following code:

import mp3play
try:

    from Tkinter import *
except ImportError:

    from tkinter import *

root = Tk()

def playMusic(root):
    filename = r'D:\My Documents\School Work\A2 Computing\Project\Westerado.mp3'
    mp3 = mp3play.load(filename)
    mp3.play()

# Declaring the buttons
button1 = Button(text="Play", fg="Black", height=1, width=7, command= playMusic)

and some more which is irrelevant to this error, however when I run the program the GUI wil show up as normal, except that when I click button1 I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Python2.7\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
TypeError: playMusic() takes exactly 1 argument (0 given)

I have not done much work with Python functions and button commands and therefore am uncertain of what has gone wrong although I do believe that the function playMusic 's parameter(s) are incorrect.

What do I need to change here in order to get this working? I know that if I take out the def playMusic(root): altogether, then when I run to code the music will play as it should. But it is the button click command that is incorrect.

EDIT -

I have switched to def play_music():, now I do not get any error when I click the button. Instead, the program button will look like it has frozen being clicked down as if it is trying to do something but nothing will come of it.

6
  • 2
    commands don't get passed any arguments - switch to def play_music(): (note also style-guide compliant name!) Commented Feb 19, 2015 at 12:29
  • @jonrsharpe I have switched to def play_music():, now I do not get any error when I click the button. Instead, the program button will look like it has frozen being clicked down as if it is trying to do something but nothing will come of it. Commented Feb 19, 2015 at 12:37
  • I guess your .play() method never returns. Commented Feb 19, 2015 at 12:44
  • What is the code for mp3play? Commented Feb 19, 2015 at 12:44
  • @AnttiHaapala The code for mp3play is: import mp3play filename = r'D:\My Documents\School Work\A2 Computing\Project\Westerado.mp3' mp3 = mp3play.load(filename) mp3.play() obviously the file path will differ for each person. The documentation for mp3play can be found here: code.google.com/p/mp3play/wiki/Examples Commented Feb 19, 2015 at 12:53

1 Answer 1

1

I was playing around with the code to try and get something to work and I found that if I made this:

filename = r'D:\My Documents\School Work\A2 Computing\Project\Westerado.mp3'
mp3 = mp3play.load(filename)

global; and then within the function just have this:

def play_music():
    mp3.play()

then when I go to click the button "play" it works perfectly fine!

Not sure why this works and the other way did not! But right now I'm not complaining.

So overall it looks like:

filename = r'D:\My Documents\School Work\A2 Computing\Project\Westerado.mp3'
mp3 = mp3play.load(filename)

def play_music():
    mp3.play()

# Declaring the buttons
button1 = Button(text="Play", fg="Black", height=1, width=7, command=play_music)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.