简体   繁体   English

在 python 的错误点使用哔声

[英]Using beep sound at point of error in python

I want to add beep sound when an error occurs in python script.当 python 脚本发生错误时,我想添加哔声。 I know how to add windows beep after specific line eg我知道如何在特定行之后添加 windows 哔声,例如

 duration = 1000  # milliseconds
    freq = 440  # Hz
     #some code here
    winsound.Beep(freq, duration)

Is it possible to enable beep whenever there is an error?是否可以在出现错误时启用哔声? I am using windows 10, python 3.6, and pycharm IDE.我正在使用 windows 10、python 3.6 和 pycharm Z581D6381F31F35E4F9DZ727。 I couldn't find any feature in pycharm that gives audio notification on error.我在 pycharm 中找不到任何在错误时提供音频通知的功能。

You can catch all errors globally and beep when an error occurs:您可以全局捕获所有错误并在发生错误时发出哔声:

try:
    do_something()
except:
    winsound.Beep(440, 1000)

You can use this one in Windows:您可以在 Windows 中使用这个:

import winsound
try:
    int('abc')
except Exception as e:
    winsound.PlaySound("*", winsound.SND_ALIAS)
    raise e

Replace int('abc') with your code.int('abc')替换为您的代码。

NOTE: It can be used only in "Windows".注意:它只能在“Windows”中使用。 Not applicable to Linux / Mac OS.不适用于 Linux / Mac OS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM