首先:您的操作系统是什么?根据文档winsound“访问Windows 平台提供的基本声音播放机制”。所以 winsound 只适用
在 Windows 上,因此如果您是 LINUX 或 UNIX,则必须找到另一种方法。
Seconds:“发出声音”对您意味着什么像这样的模块(或子进程):
import os
os.system('play --no-show-progress --null --channels 1 synth %s sine
%f' %( 0.1, 400))
你必须安装 beep (它是一个“高级” pc-speaker beeper),安装取决于你的系统Mac/OSX、Linux(Ubuntu/Debian、Fedora、Archlinux)、BSD?
在 Ubuntu/Debian 上:sudo apt-get install beep
更新 #2
@shahar 好吧,根据doc 你做对了。
您可以捕获 python 向您提出的错误以找出问题所在
try:
import winsound
winsound.Beep(400, 1000)
except RuntimeError:
print("The system is not able to beep the speaker")
except ImportError:
print("Can't import winsound module")
上面的代码适用于 python2.7 和 python3,但如果你的 python 版本是什么?
我在 windows 上使用了 python3.5.3,代码运行起来很神奇。