首页 > 解决方案 > pygame中的自动声音回响(意外)

问题描述

我正在 Tkinter 和 pygame 中制作一个报时时钟。但是,它与整点的声音相呼应,这不是我想要的。如何让它只发出一次声音并且没有回声?注意:变量clock.chime 是chime 模式(1、2、3、off)的int,clock 是tkinter 的主窗口。这是我的代码:

while True:
    try:
        d = datetime.now() #updates time
        hour = d.strftime("%I")
        mins = d.strftime("%M")
        secs = d.strftime("%S")
        day =  d.strftime("%A")
        clock.update()
        if mins=='00' and secs=='00' and clock.chime>0 and pygame.mixer.music.get_busy()==False: #chimes on the hour, but echoing
            try:
                pygame.mixer.music.load(str(clock.chime)+'.wav')
                pygame.mixer.music.play()
            except:
                messagebox.showerror('Error', 'File '+str(clock.chime)+'.wav cannot be found.') #alerts if chime file is missing
        time.sleep(0.01)
    except: #detects when window is closed
        break

标签: pygame

解决方案


推荐阅读