为什么print(\ a)在IDLE中不起作用 [英] Why isn't print(\a) working in IDLE

查看:472
本文介绍了为什么print(\ a)在IDLE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我对q做了些微改动,以使其不再重复.抱歉.

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.

我在OS X上.

类似线程中,我发现print '\a'可能有效,但是在空闲这只会打印[]

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

为什么这在IDLE中不起作用

Why does this not work in IDLE

推荐答案

之所以不会发出蜂鸣声,是因为\a(或^G)是终端铃声.由程序处理标准输出将其转换为声音. Terminal.app将播放声音(除非您将其配置为执行可视铃声",而不是将其完全关闭),但是Idle不会.而且,当然,如果您没有tty来运行,您将一无所获.

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.

如果您不介意使用PyObjC(它在所有最新版本的OS X上预装有Apple安装的Python):

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()

当然,这会播放OS X系统的蜂鸣声,而不是终端铃声.除了可能会发出不同的声音外,这意味着如果在终端中禁用铃声,您的脚本仍会发出蜂鸣声. (如果您确实需要终端机铃声,则可以始终通过例如ScriptingBridge编写终端机脚本.但我认为您不在乎.)

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

这篇关于为什么print(\ a)在IDLE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆