Why isn't print(\a) working in IDLE

天大地大妈咪最大 提交于 2019-12-06 04:42:06

问题


NOTE I changed the q slightly so that it is not a duplicate anymore. Sorry.

I have these time-consuming bioinformatics scripts I am running. I'd like them to sound a beep when they are done.

I am on OS X.

In a similar thread I found that print '\a' might work, but in Idle this just prints []

Why does this not work in IDLE


回答1:


The reason it doesn't beep is that \a (or ^G) is the terminal bell code; it's up to the program handling stdout to turn that into a sound. Terminal.app will play a sound (unless you configure it to do "visual bell" instead, of turn it off entirely), but Idle will not. And, of course, if you're running without a tty, you get nothing.

If you don't mind using PyObjC (which comes pre-installed with the Apple-installed Pythons on all recent versions of OS X):

import Cocoa
Cocoa.NSBeep()

Of course this plays the OS X system beep, not the Terminal bell. Besides possibly being a different sound, this means if you disable the bell in Terminal, your script will still beep. (If you really want a Terminal bell, you can always script Terminal via, e.g., ScriptingBridge. But I don't think you care.)




回答2:


This tiny Python snippet using afplay does what I need: ten loud-ish dings at the end of a program:

from os import system
for i in range(0,10):
    system('afplay /System/Library/Sounds/Glass.aiff')

I presume the overhead of importing system is not small, but it works for me



来源:https://stackoverflow.com/questions/13313440/why-isnt-print-a-working-in-idle

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