适用于不带终端的windows或Mac的Python铃声

适用于不带终端的windows或Mac的Python铃声,python,audio,terminal,stdout,Python,Audio,Terminal,Stdout,我有一个程序,我想使用内部铃声“\a”,因为它可以在linux、windows和Mac上运行,而不需要额外的文件。当程序以.py运行时效果很好,但是,我也希望我的程序在没有打开的终端窗口的情况下运行,因此当程序以.pyw运行时它不工作,因为程序没有连接到stdout 现在,我使用sys.stdout.write('\a')发出警报,但如果没有将程序连接到终端,则不会发出警报(并最终导致程序崩溃) 同样,我想做一些所有终端系统都默认的事情,所以我尽量避免包含声音文件并播放它。假设platform.

我有一个程序,我想使用内部铃声“\a”,因为它可以在linux、windows和Mac上运行,而不需要额外的文件。当程序以.py运行时效果很好,但是,我也希望我的程序在没有打开的终端窗口的情况下运行,因此当程序以.pyw运行时它不工作,因为程序没有连接到stdout

现在,我使用sys.stdout.write('\a')发出警报,但如果没有将程序连接到终端,则不会发出警报(并最终导致程序崩溃)

同样,我想做一些所有终端系统都默认的事情,所以我尽量避免包含声音文件并播放它。

假设platform.system()为您提供了操作系统的名称,您可以在Linux和Windows上使用以下命令。它给你一些选择特定声音的自由

if platform.system () == 'Linux':
    import pyaudio
else:
    import winsound

class BeepBase:
    def __init__ (self):
        self.normalPars = (1000, 0.1)
        self.specialPars = (2100, 0.1)
        self.attentionPars = (400, 0.5)

if platform.system () == 'Linux':
    class Beep (BeepBase):
        def __init__ (self):
            BeepBase.__init__ (self)

            self.sampleFreq = 16000
            self.sampleTime = 1. / self.sampleFreq
            self.attackTime = 0.001
            self.decayTime = 0.015

            self.normalWave = self.getWave (*self.normalPars)
            self.specialWave = self.getWave (*self.specialPars) 
            self.attentionWave = self.getWave (*self.attentionPars)

            # self.pyAudio = pyaudio.PyAudio () # Gives errors

        def done (self):    # Never called, may leak resources
            self.pyAudio.terminate ()

        def getWave (self, frequency, duration):
            wave = ''.join ([
                chr (int (128 + 127 * math.sin (2 * math.pi * frequency * time) * max (0, min (1, min (time/self.attackTime, (duration - time)/self.decayTime)))))
                for time in [self.sampleTime * iSample for iSample in xrange (int (duration / self.sampleTime))]
            ])

            return wave

        def any (self, wave):
            self.stream = self.pyAudio.open (
                format = self.pyAudio.get_format_from_width (1),
                channels = 1,
                rate = self.sampleFreq,
                output = True
            )
            sleep (0.025)
            self.stream.write (wave)
            sleep (0.15)
            self.stream.stop_stream ()
            self.stream.close ()

        def normal (self):
            if main.settings.sound:
                self.any (self.normalWave)

        def special (self):
            if main.settings.sound:
                self.any (self.specialWave)

        def attention (self):
            if main.settings.sound:
                self.any (self.attentionWave)
else:
    class Beep (BeepBase):
        def normal (self):
            if main.settings.sound:
                winsound.Beep (self.normalPars [0], int (self.normalPars [1] * 1000))

        def special (self):
            if main.settings.sound:
                winsound.Beep (self.specialPars [0], int (self.specialPars [1] * 1000))     

        def attention (self):
            if main.settings.sound:
                winsound.Beep (self.attentionPars [0], int (self.attentionPars [1] * 1000))

beep = Beep ()
我发现有句谚语说,下面的代码片段可以工作:

import Tkinter
Tkinter.Tk().bell()
我不确定它是否有效。我在Linux上,无法使您的命令在我的计算机上运行(可能是管理员禁用了该命令或其他命令)

请注意,上面的代码片段将打开一个Tkinter窗口。我认为在发出嘟嘟声后隐藏窗口并关闭它不会有什么大问题(请稍等,或者bell()有一种事件侦听器)


对不起,没有足够的细节。我只是希望我能给你一个提示,因为我不能自己测试它。祝你好运!:)

太好了,我已经在用Tkinter了。在没有终端的情况下试过,第一次就成功了。谢谢