代码完成时发出声音警报 [英] Sound alarm when code finishes
问题描述
我处于这样一种情况下,我的代码需要花费很长时间才能运行,并且我不想一直盯着它,但想知道它何时完成。
I am in a situation where my code takes extremely long to run and I don't want to be staring at it all the time but want to know when it is done.
完成后如何使(Python)代码听起来像是警报?我正在考虑使其在代码末尾时播放.wav文件...
How can I make the (Python) code sort of sound an "alarm" when it is done? I was contemplating making it play a .wav file when it reaches the end of the code...
这甚至是可行的主意吗?
如果可以,我该怎么做?
Is this even a feasible idea? If so, how could I do it?
推荐答案
在Windows上
On Windows
import winsound
duration = 1000 # milliseconds
freq = 440 # Hz
winsound.Beep(freq, duration)
其中频率是Hz的频率,持续时间是毫秒。
Where freq is the frequency in Hz and the duration is in milliseconds.
import os
duration = 1 # seconds
freq = 440 # Hz
os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))
要使用此示例,必须安装 sox
。
In order to use this example, you must install sox
.
在Debian / Ubuntu / Linux Mint上,在您的终端上运行此命令:
On Debian / Ubuntu / Linux Mint, run this in your terminal:
sudo apt install sox
在Mac上,在终端机中运行(使用macports):
On Mac, run this in your terminal (using macports):
sudo port install sox
Mac上的语音
Speech on Mac
import os
os.system('say "your program has finished"')
Linux上的语音
Speech on Linux
import os
os.system('spd-say "your program has finished"')
您需要在Ubuntu中安装 speech-dispatcher
软件包(或其他发行版中的相应软件包):
You need to install the speech-dispatcher
package in Ubuntu (or the corresponding package on other distributions):
sudo apt install speech-dispatcher
这篇关于代码完成时发出声音警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!