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

本文介绍了如何在不同操作系统上(包括Windows、Linux和Mac)使用Python实现声音播放及完成任务后的通知功能。针对每种操作系统,提供了具体的代码示例,并说明了所需的额外软件包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值