在没有外部库的情况下用 python 播放简单的哔哔声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4467240/
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 15:52:26  来源:igfitidea点击:

Play simple beep with python without external library

pythonplaybacknoise

提问by a sandwhich

Using only the modules that come with a standard python 2.6 installation, would it be possible to play a simple beeping noise?

仅使用标准python 2.6安装附带的模块,是否可以播放简单的哔哔声?

采纳答案by David Wolever

If you're on a Unix terminal, you can print "\a" to get a terminal bell:

如果您使用的是 Unix 终端,您可以打印“\a”来获得终端铃声:

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

Of course, that will print a newline too… So sys.stdout.write("\a")might be better. But you get the idea.

当然,这也会打印一个换行符……所以sys.stdout.write("\a")可能会更好。但是你明白了。

回答by user3394963

On windows:

在窗户上:

import winsound         # for sound  
import time             # for sleep

winsound.Beep(440, 250) # frequency, duration
time.sleep(0.25)        # in seconds (0.25 is 250ms)

winsound.Beep(600, 250)
time.sleep(0.25)

34.4. winsound — Sound-playing interface for Windows:

34.4. winsound — Windows 的声音播放界面:

http://docs.python.org/2.6/search.html?q=sound&check_keywords=yes&area=default

http://docs.python.org/2.6/search.html?q=sound&check_keywords=yes&area=default

See also: Clear screen and beep for various platforms. (Python recipe) http://code.activestate.com/recipes/577588-clear-screen-and-beep-for-various-platforms/

另请参阅:各种平台的清除屏幕和蜂鸣声。(Python 配方) http://code.activestate.com/recipes/577588-clear-screen-and-beep-for-various-platforms/