从导入的模块中的函数更改字符串变量

时间:2018-12-15 04:22:20

标签: python tkinter global-variables speech-recognition

我正在制作一个python Tkinter GUI,它将实时将音频从麦克风转录为文本,同时,如果说出并检测到“哦,不”或“ erm”字样,则在屏幕上显示警告消息。我将语音识别脚本transcribe.py作为模块导入到主程序sample.py中。我的问题是:在导入的模块sample.py中检测到单词“ erm”时,如何在主程序transcribe.py中更改tk.Label的文本?

下面是我的代码。

#sample.py(class of the window that the warning message should be shown on)

import transcribe.py

class PracticePage(tk.Frame, App):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Frame.config(self, bg="white")

        global label_var 
        label_var = tk.StringVar()
        label_var.set("")
        self.timeLeft = tk.Label(self, textvariable= label_var, font ="遊ゴジックLight 20", bg ="white")

        self.timeLeft.place(relwidth = 0.4, relheight = 0.1, relx = 0.5, rely = 0.45, anchor = "center")

这是我的语音识别模块中代码的一部分。

#transcribe.py
def listen_print_loop(responses): #responses is the audio stream from microphone
spoken = []

for response in responses:
    if not response.results:
        continue

    result = response.results[0]
    if not result.alternatives:
        continue

    transcript = result.alternatives[0].transcript
    overwrite_chars = ' ' * (num_chars_printed - len(transcript))

    spoken.append(transcript + overwrite_chars)

    if "erm" in spoken:
        #change the text of label in sample.py here

    if not result.is_final:
        num_chars_printed = len(transcript)

    else:
        spoken.append(transcript + overwrite_chars)
        final_spoken = ''.join(spoken) 
        print(final_spoken)

我是python初学者,对我的问题感到困惑时,抱歉。如果您什么都不懂,请进一步询问我。谢谢。

0 个答案:

没有答案