我需要一种方法来通过按下按钮停止 运行 的功能

I need a way to stop a function from running at the push of a button

我已经为此工作了几个小时,而且我已经尝试了从 if 语句中的 break 到制作索引变量的所有方法。我似乎找不到阻止 sound/music 播放的方法。我的目标是按 'Q' 并停止函数和 while 循环以启动另一个 while 循环。

import keyboard
import time
from playsound import playsound

#this handles the name that is displayed and makes it easier to input
def Song(type,opt):
        time = 1
        print(opt)
        playsound(type)

print('''Enter Your Music Selection:
O) Overworld
F) Fighting
B) Boss-Strong Enemy
V) Victory
I) Inside-Taven-Shop
D) Dungion
L) Lose-Death
    ''')

while True:
    while keyboard.is_pressed('O'):
        Song('MusicStuff/test0.wav','Overworld Theme')

    while keyboard.is_pressed('F'):
        Song('MusicStuff/TrashLine.wav','Fighting Theme')

Playsound 由一个函数组成,它只播放声音,不做任何其他事情。这就是您无法“停止”该功能的原因。

如果你想要更多的交互性,你必须使用不同的库。你可以使用pyaudio。

看看这个 post。