简体   繁体   English

为什么在代码的rest之前播放声音?

[英]Why does the sound play before the rest of the code?

I want to have the block turn green and the sound play together, or if that's not possible then I would like to have the block turn green first.我想让方块变绿并一起播放声音,或者如果这不可能,那么我想让方块先变绿。 but I can't find out why the sound is playing first... Here is my code:但我不知道为什么声音首先播放......这是我的代码:

def number(*args):
try:
    x = r.get()
    if x <= '3':
        s.configure('Danger.TFrame', background='green',
                    borderwidth=5, relief='raised')
        ttk.Frame(root, width=200, height=200, style='Danger.TFrame')
        winsound.PlaySound('dotto.wav', winsound.SND_FILENAME)
        print("green")
    elif x >= "5":
        s.configure('Danger.TFrame', background='red',
                    borderwidth=5, relief='raised')
        ttk.Frame(root, width=200, height=200, style='Danger.TFrame')
        print("red")
    else:
        s.configure('Danger.TFrame', background='orange',
                    borderwidth=5, relief='raised')
        ttk.Frame(root, width=200, height=200, style='Danger.TFrame')
        print("orange")
except:
    print("error")

I am using winsound, and the file thats playing is dotto.wav.我用的是winsound,播放的文件是dotto.wav。

Thanks,谢谢,

winsound.PlaySound() is a blocking method call by default, which means it will wait until playing the sound is finished before continuing with anything else. winsound.PlaySound()默认情况下是一个阻塞方法调用,这意味着它会等到声音播放完成后再继续其他任何事情。 In your case "anything else" probably includes rendering the (now green) block.在您的情况下,“其他任何东西”可能包括渲染(现在为绿色)块。

So in your example code, your Block is changed to green before starting to play the sound, but you probably can't see that, because it isn't re-drawn on the screen, until after the sound has finished.因此,在您的示例代码中,您的 Block开始播放声音之前变为绿色,但您可能看不到,因为它不会在屏幕上重新绘制,直到声音结束后。

To tell winsound you don't want to wait for the sound to end, you need to pass the winsound.SND_ASYNC parameter.要告诉 winsound 您不想等待声音结束,您需要传递winsound.SND_ASYNC参数。

winsound.PlaySound('dotto.wav', winsound.SND_FILENAME | winsound.SND_ASYNC)

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

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