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

我怎样才能使(Python)代码在完成时发出“警报”?我正在考虑让它在代码结束时播放一个.wav文件……

这个想法可行吗? 如果是,我该怎么做呢?


当前回答

Ubuntu语音调度器可以使用:

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

其他回答

参见:Python Sound(“Bell”) 当我想做同样的事情时,这帮助了我。 所有学分归gbc所有

引用:

你有没有试过:

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

这在Mac OS 10.5上也适用

实际上,我认为你最初的尝试也可以,只要稍加修改:

print('\a')

(您只需要在字符序列周围加上单引号)。

import subprocess

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

我创建了一个python模块,在代码执行后提醒Telegram应用程序上的开发人员。这可能比发出警报声音更有帮助。

Pypi链接: https://pypi.org/project/devreminder/

Github链接: https://github.com/cagataygulten/devreminder

例子:

In [1]>>
    from devreminder import DevReminder
    import time

In [2]>>
    remind = DevReminder(1932126911,False,0)

In [3]>>
    remind.me("Example")
    time.sleep(6)

输出:

请按照README文件了解更多信息。

在macOS上有一个非常简单的方法来做到这一点。

python file.py && say done

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

import winsound
winsound.MessageBeep()