Python:如何在代码完成时发出警报声?

在Windows上

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

其中,FREQ是频率(以赫兹为单位),而持续时间是毫秒(毫秒)。

在Linux(和Mac)上

import os
duration = 1  # second
freq = 440  # Hz
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (duration, freq))

要使用此示例,必须安装sox...

在Debian/Ubuntu/LinuxMint上,您需要在终端中运行:

sudo apt install sox

这是Macport的方法...运行这是您的终端:

sudo port install sox

在Mac上

如果你在终端上使用Mac,也许在Windows上也能做同样的事情,但我只知道Mac的情况:

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

在Linux上

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

您需要安装speech-dispatcherUbuntu中的包(或其他发行版上的相应包):

sudo apt install speech-dispatcher

赞0评论0收藏0分享

在Windows上 import winsound duration = 1000 # millisecond freq = 440 # Hz winsound.Beep(freq, duration) 其中,FREQ是频率(以赫兹为单位),而持续时间是毫秒(毫秒)。 在L...

用户回答回答于 2018-03-012018-03-01 17:11:16

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

在Windows中,可以在末尾放:

import winsound
winsound.Beep(500,1000)

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

猜你喜欢

转载自blog.csdn.net/countofdane/article/details/82315244