【问题标题】:How to keep playing sound files before the check button clicks in python?如何在python中单击检查按钮之前继续播放声音文件?
【发布时间】:2021-01-19 01:37:36
【问题描述】:

我现在正在尝试用python制作一个听写语音的程序。 我已经使用 tkinter 输入了语音输入听写、answer_check 按钮、重置按钮, 并定义了initial()、answer_check()、Reset()的函数。 它在不播放语音的情况下运行良好,这是一个句子的 mp3 文件。 我在每个函数中间插入了 playsound('*.mp3'),即 initial() 和 Reset()。 其实我想在GUI工作后播放声音文件,听写继续,完成听写后单击检查按钮,然后停止播放声音同时显示答案。 但这并没有发生。命令播放声音,出现Entry GUI,输入听写,检查答案。我已经认识到原因。完成其功能后出现GUI,但执行功能时出现播放声音。这样理解对吗? 于是我定义了一个播放声音的新函数MyPlaySound(): 我现在在问如何在满足条件时停止播放声音文件(单击检查或重置按钮)。这是仅显示我制作的函数的代码,但它已进入无限循环。 请让我知道如何制作这些函数,尤其是 MyPlaySound() 函数,因为我不确定其中的语法,所以该函数有一些错误。 我知道这对专家来说很容易。我花了一整天,但我不确定要花多少天来解决这个问题而不帮助你专家。提前致谢。 '''

功能

def initial():
global shuffled_words, play_num, play_num_row, given_words, ans, u_num_list
label1.configure(text=shuffled_words[play_num_row])
MyPlaySound()

def ans_check():
global shuffled_words, play_num, play_num_row, given_words, ans, u_num_list
ans = str()
ans = ' '.join(given_words[play_num_row])
user_input = e1.get()
if user_input == ans:
    msgbox.showinfo("Success", "Yeop, this is right")
    label2.configure(text=given_words[play_num_row])
else:
    msgbox.showinfo("Error", "Nop, this is not right")
    e1.delete(0, END)
    label2.configure(text=given_words[play_num_row])


def Reset():
global shuffled_words, play_num, play_num_row, given_words, ans, u_num_list
play_num_row = random.randint(0, len(given_words)-1)
play_num = u_num_list[play_num_row]
label1.configure(text=shuffled_words[play_num_row])
label2.config(text="")
e1.delete(0, END)
MyPlaySound()


def MyPlaySound():
global shuffled_words, play_num, play_num_row, given_words, ans, u_num_list
i = 0
while True:
    playsound(str(play_num)+'.mp3')
    i = i + 1
    if ans_check() == True:
        break
    elif Reset() == True:
        break

'''

【问题讨论】:

    标签: python


    【解决方案1】:

    虽然我没有得到你的要求,但仍然使用线程同时执行多个任务。

    import threading
    def func():
        #function/task to be threaded
    
    thread1=threading.Thread(target=func)
    thread1.start()
    
    #To stop the thread
    thread1.join()
    

    【讨论】:

    • 谢谢。正如您告诉我的那样,我对线程概念有了一些了解。将其应用于程序后,它可以工作。但是 thread1.join() 不能停止线程。但是,我将尝试更多地理解它并应用于我的程序。我非常感谢并感谢您向我提供信息。我希望你好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多