![](/img/trans.png)
[英]Python: what are the nearest Linux and OSX equivalents of winsound.Beep?
[英]Configure Eclipse to Beep when done (Python, Linux)
我完成后將我的應用程序配置為蜂鳴聲(幫助我在長時間運行中執行多任務)。 在Windows上很簡單:
def beep_please():
"""Beep on Windows"""
if os.name == 'nt':
import winsound #@UnresolvedImport
winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
import atexit
atexit.register(beep_please)
問題是我最近切換到Linux並且簡單的嗶聲不起作用。 打印'\\a'
也不起作用。 救命?
根本原因是大多數現代Linux發行版都關閉了煩人的默認“嘟嘟”聲。
潛在的解決方案是使用pygame,或直接使用已安裝的“播放器”之一。
使用Pygame看起來像這樣:
import pygame
pygame.init()
pygame.mixer.music.load("my_sound_file.ogg")
pygame.mixer.music.play()
pygame.event.wait()
但是為了非運行時實用程序,我不想要新的外部依賴,所以我最終做的是:
import os
os.system("/usr/bin/canberra-gtk-play --id='system-ready'")
Ubuntu主題中還有很多其他聲音文件:
ls /usr/share/sounds/ubuntu/stereo
嘗試執行sys.stdout.write('\\007')
而不是print '\\a'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.