2014-10-04 31 views
0
import subprocess 

def musicplayer(): 
audio_file = "C:\\Python34\\Calvin.mp3" 

return_code = subprocess.call(["afplay", audio_file]) 

button2 = Button(root, text="Place Bet", command=musicplayer) 
button2.configure(background="darkred") 
button2.pack(anchor=E) 

我已經將音樂文件移動到python的更容易訪問的區域,因爲它以前沒有工作過。我對編碼相當陌生,只是試圖讓一些小程序進行。這是正確的嗎?我不斷收到錯誤:試圖在程序中播放音樂 - 文件路徑錯誤

"Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__ 
    return self.func(*args) 
    File "C:/Users/Michael/Documents/test 1.py", line 27, in musicplayer 
    return_code = subprocess.call(["afplay", audio_file]) 
    File "C:\Python34\lib\subprocess.py", line 537, in call 
    with Popen(*popenargs, **kwargs) as p: 
    File "C:\Python34\lib\subprocess.py", line 858, in __init__ 
    restore_signals, start_new_session) 
    File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

如果任何人都可以協助或告訴我放棄,那太棒了!

+0

什麼/在哪裏'afplay'? – 2014-10-04 15:47:01

+0

一個朋友添加了它,不知道爲什麼...刪除它,現在得到錯誤:OSError:[WinError 193]%1不是有效的Win32應用程序 – Hagrid 2014-10-04 15:51:49

+0

那麼,你需要某種庫或可執行文件來播放音頻文件。 'afplay'似乎只是一個OS X的應用程序,所以你甚至在你的Windows系統上都沒有。 – 2014-10-04 15:54:20

回答

0

檢查路徑,文件名大小寫,並可能使用原始字符串R「C:\ Python34 \ Calvin.mp3」

+0

檢查所有的大小寫和文件路徑等與afplay刪除我仍然得到錯誤:OSError:[WinError 193]%1不是有效的Win32應用程序和afplay回來我得到的錯誤:FileNotFoundError:[WinError 2]系統找不到指定的文件 – Hagrid 2014-10-04 15:54:47

+0

抱歉,看起來很難看... – Hagrid 2014-10-04 15:55:09

0

似乎afplay是不是在蟒蛇的路徑,給的全路徑afplay和您的腳本將工作

subprocess.call(["c:\\path\\to\\afplay.exe", audio_file])