简体   繁体   English

Python TQDM进度栏阻止Winsound

[英]Python tqdm progress bar blocks winsound

Im experiencing some problems with winsound and tqdm. 我在使用winsound和tqdm时遇到了一些问题。 Im making an underground controlling system using the progress bar showing the distance between 2 stations and winsound playing the name of the station. 我使用进度条制作了一个地下控制系统,该进度条显示了两个站点之间的距离,并播放了名为该站点名称的winsound。 The progress bar shows up but there is no sound. 显示进度条,但没有声音。

from tqdm import tqdm
import time
import winsound

for i in tqdm(range(100)):
    time.sleep(0.02)

winsound.PlaySound("Nastepna.wav", winsound.SND_ASYNC)

however when I do this: 但是,当我这样做时:

from tqdm import tqdm
import time
import winsound

winsound.PlaySound("Nastepna.wav", winsound.SND_ASYNC)

for i in tqdm(range(100)):
    time.sleep(0.02)

the sound plays with no problems. 声音播放没有问题。

From the winsound documentation on SND_ASYNC : SND_ASYNCwinsound 文档中:

winsound.SND_ASYNC
    Return immediately, allowing sounds to play asynchronously.

So the SND_ASYNC flag makes the call to PlaySound asynchronous. 因此, SND_ASYNC标志使对PlaySound的调用异步。 That is, it does not wait for the sound to complete before returning. 也就是说,它不等待声音完成再返回。 This works fine when you make the call first and then effectively sleep for 2 seconds displaying the progress bar, because the sound has time to play out while the program continues to execute. 当您首先拨打电话,然后在进度条上有效休眠2秒钟时,此方法效果很好,因为在程序继续执行时声音有时间播放。

But when you play the sound in this way after the work of the program is done, the PlaySound function immediately returns and then the program has nothing else to do, so it exits, providing no time for the sound to play. 但是,当您在程序工作完成后以这种方式播放声音时, PlaySound函数会立即返回,然后该程序无需执行任何其他操作,因此它退出了,没有时间播放声音。

You can change this behavior by passing the winsound.SND_FILENAME flag to PlaySound instead, which will make the call synchronous , waiting for the sound to finish playing before returning: 你可以通过改变此行为winsound.SND_FILENAME标志PlaySound代替,这将使得呼叫同步 ,等待声音完成返回前玩:

from tqdm import tqdm
import time
import winsound

for i in tqdm(range(100)):
    time.sleep(0.02)

winsound.PlaySound("Nastepna.wav", winsound.SND_FILENAME)

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

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