Python 代码完成时发出声音警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16573051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:02:21  来源:igfitidea点击:

Sound alarm when code finishes

pythonalarmaudio

提问by mtigger

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.

我的代码需要很长时间才能运行,我不想一直盯着它,但想知道它什么时候完成。

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...

完成后,如何使(Python)代码听起来像“警报”?我正在考虑让它在到达代码末尾时播放 .wav 文件......

Is this even a feasible idea? If so, how could I do it?

这甚至是一个可行的想法吗?如果是这样,我该怎么做?

采纳答案by Ryan Saxe

On Windows

在 Windows 上

import winsound
duration = 1000  # milliseconds
freq = 440  # Hz
winsound.Beep(freq, duration)

Where freq is the frequency in Hz and the duration is in milliseconds.

其中 freq 是以赫兹为单位的频率,持续时间以毫秒为单位。

On Linux and Mac

在 Linux 和 Mac 上

import os
duration = 1  # seconds
freq = 440  # Hz
os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))

In order to use this example, you must install sox.

为了使用这个例子,你必须安装sox.

On Debian / Ubuntu / Linux Mint, run this in your terminal:

在 Debian / Ubuntu / Linux Mint 上,在你的终端中运行:

sudo apt install sox

On Mac, run this in your terminal (using macports):

在 Mac 上,在终端中运行它(使用 macports):

sudo port install sox

Speech on Mac

Mac 上的演讲

import os
os.system('say "your program has finished"')

Speech on Linux

在 Linux 上的演讲

import os
os.system('spd-say "your program has finished"')

You need to install the speech-dispatcherpackage in Ubuntu (or the corresponding package on other distributions):

您需要speech-dispatcher在 Ubuntu 中安装该软件包(或其他发行版上的相应软件包):

sudo apt install speech-dispatcher

回答by Saullo G. P. Castro

This one seems to work on both Windows and Linux* (from this question):

这个似乎适用于 Windows 和 Linux*(来自这个问题):

def beep():
    print("\a")

beep()

In Windows, can put at the end:

在Windows中,可以放在最后:

import winsound
winsound.Beep(500, 1000)

where 500 is the frequency in Herz
      1000 is the duration in miliseconds

To work on Linux, you may need to do the following (from QO's comment):

要在 Linux 上工作,您可能需要执行以下操作(来自 QO 的评论):

  • in a terminal, type 'cd /etc/modprobe.d' then 'gksudo gedit blacklist.conf'
  • comment the line that says 'blacklist pcspkr', then reboot
  • check also that the terminal preferences has the 'Terminal Bell' checked.
  • 在终端中,输入“cd /etc/modprobe.d”,然后输入“gksudo gedit blacklist.conf”
  • 注释“blacklist pcspkr”行,然后重新启动
  • 还要检查终端首选项是否选中了“终端铃声”。

回答by Kuchi

See: Python Sound ("Bell")
This helped me when i wanted to do the same.
All credits go to gbc

请参阅:Python Sound ("Bell")
当我想做同样的事情时,这对我有帮助。
所有学分都归 gbc

Quote:

引用:

Have you tried :

你有没有尝试过 :

import sys
sys.stdout.write('\a')
sys.stdout.flush()

That works for me here on Mac OS 10.5

这在 Mac OS 10.5 上对我有用

Actually, I think your original attempt works also with a little modification:

实际上,我认为您最初的尝试也可以进行一些修改:

print('\a')

(You just need the single quotes around the character sequence).

(您只需要围绕字符序列的单引号)。

回答by zekel

Kuchi's answerdidn't work for me on OS X Yosemite (10.10.1). I did find the afplaycommand (here), which you can just call from Python. This works regardless of whether the Terminal audible bell is enabled and without a third-party library.

Kuchi 的回答在 OS X Yosemite (10.10.1) 上对我不起作用。我确实找到了afplay命令(这里),你可以从 Python 调用它。无论是否启用终端可听铃声并且没有第三方库,这都有效。

import os
os.system('afplay /System/Library/Sounds/Sosumi.aiff')

回答by Ishan Khare

ubuntu speech dispatcher can be used:

可以使用 ubuntu 语音调度器:

import subprocess
subprocess.call(['speech-dispatcher'])        #start speech dispatcher
subprocess.call(['spd-say', '"your process has finished"'])

回答by Josh Allemon

 print('
import subprocess

subprocess.call(['D:\greensoft\TTPlayer\TTPlayer.exe', "E:\stridevampaclip.mp3"])
7')

plays the bell sound

播放铃声

回答by Du Peng

python myscript.py && 
    notify-send 'Alert' 'Your task is complete' && 
    paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga

回答by rtaft

Why use python at all? You might forget to remove it and check it into a repository. Just run your python command with && and another command to run to do the alerting.

为什么要使用python?您可能忘记删除它并将其签入存储库。只需使用 && 和另一个命令运行你的 python 命令来运行警报。

function apython() {
    /usr/bin/python $*
    notify-send 'Alert' "python $* is complete"
    paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
}

or drop a function into your .bashrc. I use apython here but you could override 'python'

或将函数放入您的 .bashrc。我在这里使用 apython 但你可以覆盖 'python'

import winsound
winsound.MessageBeep()

回答by poulter7

I'm assuming you want the standard system bell, and don't want to concern yourself with frequencies and durations etc., you just want the standard windows bell.

我假设你想要标准的系统铃,不想关心频率和持续时间等,你只想要标准的窗户铃。

import time
time.sleep(10)   #Set the time
for x in range(60):  
    time.sleep(1)
    print('\a')

回答by Satyendra Yadav

It can be done by code as follows:

可以通过如下代码完成:

##代码##