Я делаю графический интерфейс Python Tkinter, который будет транскрибировать звук с микрофона в текст в реальном времени и в то же время показывать предупреждающее сообщение на экране, если произнесено и обнаружено слово «о нет» или «erm».Я импортировал скрипт распознавания речи transcribe.py
как модуль в основную программу sample.py
.У меня вопрос: как я могу изменить текст tk.Label в моей основной программе sample.py
, если в импортированном модуле обнаружено слово "erm" transcribe.py
?
Ниже приведен мой код.
#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, и мне жаль, если мой вопрос сбивает с толку.Пожалуйста, спросите меня, если вы ничего не понимаете.Спасибо.