gpt4 book ai didi

python - 按钮释放 Tkinter

转载 作者:行者123 更新时间:2023-12-01 05:44:27 24 4
gpt4 key购买 nike

这是我的情况。我有两个 tkinter 按钮:

  1. 播放按钮:播放声音
  2. 停止按钮:播放期间停止声音

代码如下:

def Play(self):
//plays a file with pygame module

def Stop(self):
//Stop using pygame stop


Button( root, text ='Play', command= Play).pack()
Button( root, text ='Stop', command= Stop).pack()

问题是,当我单击“播放”按钮时,它会一直按下,直到完成声音播放,卡住整个程序,不允许我在此期间按“停止”按钮。

如何防止程序在播放过程中卡住,以便我可以执行其他操作(例如停止)?

最佳答案

防止 GUI 在播放歌曲时卡住的唯一方法是使用单独的线程或单独的进程来播放歌曲。只要播放歌曲的函数正在运行并且在主线程中,事件循环就无法运行。如果事件循环无法运行,GUI 将被卡住。

关于python - 按钮释放 Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com