如何在 python 3.6 中播放 .wav 文件?
How can I play a .wav file in python 3.6?
目前,我正在使用 python 3.6 制作一个程序,我一直在尝试在按下按钮后产生声音效果。我从另一个问题中找到了一些东西,发现 here。下面是我的代码。
from tkinter import *
window = Tk()
c = Canvas(window, height=100, width=100, bg='blue')
c.pack()
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3
Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
data = base64.b64encode(f.read())
def playSound():
sound = winsound.PlaySound(base64.b64decode(data), winsound.SND_MEMORY)
def sound(event):
global sound
if event.keysym == 'Up':
playSound()
c.pack('<Key>', sound)
然后我收到这个错误信息:
RESTART: C:\Users\lenonvo\AppData\Local\Programs\Python\Python36\Python 3
Files\Python 3.6.3\Test2.py
Traceback (most recent call last):
File "C:\Users\lenonvo\AppData\Local\Programs\Python\Python36\Python 3
Files\Python 3.6.3\Test2.py", line 7, in <module>
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python
3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
FileNotFoundError: [Errno 2] No such file or directory:
'Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3
Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav'
有什么建议吗?
此外,我不是高级 python 编码员,所以如果您的回答可以简单化,我们将不胜感激。 =]
感谢 Patrick Artner,现在有一个问题已解决,但现在出现此错误消息:
data = base64.b64decode(f.read())
NameError: name 'base64' is not defined
这个:
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
是程序运行位置的相对路径。
使用
with open('C:/Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
^^^^
目前,我正在使用 python 3.6 制作一个程序,我一直在尝试在按下按钮后产生声音效果。我从另一个问题中找到了一些东西,发现 here。下面是我的代码。
from tkinter import *
window = Tk()
c = Canvas(window, height=100, width=100, bg='blue')
c.pack()
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3
Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
data = base64.b64encode(f.read())
def playSound():
sound = winsound.PlaySound(base64.b64decode(data), winsound.SND_MEMORY)
def sound(event):
global sound
if event.keysym == 'Up':
playSound()
c.pack('<Key>', sound)
然后我收到这个错误信息:
RESTART: C:\Users\lenonvo\AppData\Local\Programs\Python\Python36\Python 3
Files\Python 3.6.3\Test2.py
Traceback (most recent call last):
File "C:\Users\lenonvo\AppData\Local\Programs\Python\Python36\Python 3
Files\Python 3.6.3\Test2.py", line 7, in <module>
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python
3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
FileNotFoundError: [Errno 2] No such file or directory:
'Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3
Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav'
有什么建议吗?
此外,我不是高级 python 编码员,所以如果您的回答可以简单化,我们将不胜感激。 =]
感谢 Patrick Artner,现在有一个问题已解决,但现在出现此错误消息:
data = base64.b64decode(f.read())
NameError: name 'base64' is not defined
这个:
with open('Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
是程序运行位置的相对路径。
使用
with open('C:/Users/lenonvo/AppData/Local/Programs/Python/Python 3.6/Python 3 Files/Python [3.6.3]/Sounds/Blook Game/Attack.wav','rb') as f:
^^^^