Sound alarm when code finishes

前端 未结 11 911
离开以前
离开以前 2021-01-29 17:42

I am in a situation where my code takes extremely long to run and I don\'t want to be staring at it all the time but want to know when it is done.

How can I make the (Py

11条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 18:09

    On Windows

    import winsound
    duration = 1000  # milliseconds
    freq = 440  # Hz
    winsound.Beep(freq, duration)
    

    Where freq is the frequency in Hz and the duration is in milliseconds.

    On Linux and Mac

    import os
    duration = 1  # seconds
    freq = 440  # Hz
    os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))
    

    In order to use this example, you must install sox.

    On Debian / Ubuntu / Linux Mint, run this in your terminal:

    sudo apt install sox
    

    On Mac, run this in your terminal (using macports):

    sudo port install sox
    

    Speech on Mac

    import os
    os.system('say "your program has finished"')
    

    Speech on Linux

    import os
    os.system('spd-say "your program has finished"')
    

    You need to install the speech-dispatcher package in Ubuntu (or the corresponding package on other distributions):

    sudo apt install speech-dispatcher
    

提交回复
热议问题