Play simple beep with python without external library

痴心易碎 提交于 2019-12-29 03:27:13

问题


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


回答1:


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

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




回答2:


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:

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/



来源:https://stackoverflow.com/questions/4467240/play-simple-beep-with-python-without-external-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!