【发布时间】:2019-11-12 05:15:19
【问题描述】:
当与另一个无限循环并行运行时,Tkinter GUI 不显示;我尝试过线程和多处理技术,而在主代码中使用 GUI 并调用 livepeech 代码,反之亦然;并在函数中定义两个代码并从主线程调用。但问题仍然存在;下面附有不同的结果,虽然你发现它是评论,但也尝试过这种方法,
#*********************************** IMPORTING MODULES*****************
import tkinter
from tkinter import*
import tkinter.messagebox
import sqlite3
import os
from multiprocessing import Process
from pocketsphinx import LiveSpeech, get_model_path
import threading
from time import sleep
model_path = get_model_path()
#*************** TKINTER GUI CODE******************
def gui():
window = tkinter.Tk()
window.title("Smart Notice Board")
top = Canvas(window,width=400,height=200)
top.pack(fill=X)
button_5 = Button(text='PORTAL SYSTEM', height = 2, width=17, activebackground = '#33B5e5', bg = 'brown', fg = 'white',command = portal )
top.create_window(80,80, anchor='nw', window = button_5)
def portal():
print("2")
#**************** speech TO text CODE***************
def speech():
speech = LiveSpeech(
verbose=False,
sampling_rate=16000,
buffer_size=2048,
no_search=False,
full_utt=False,
hmm=os.path.join(model_path, 'en-us'),
lm=os.path.join(model_path, '8582.lm'),
dic=os.path.join(model_path, '8582.dict')
)
for phrase in speech:
print(phrase)
a=str(phrase)
print(a)
#************************** MAIN LOOP************************
if __name__ == "__main__":
#************ FOR THREADING************
#thread1 = threading.Thread(target=gui)
#thread2 = threading.Thread(target=speech)
#thread1.daemon = True
#thread1.start()
#thread2.start()
#thread1.join()
#thread2.join()
#************ FOR MULTIPROCESSING****************
#processes=[]
#P1 = Process(target=gui)
#P2 = Process(target=speech)
#processes.append(P1)
#processes.append(P2)
#P2.daemon = True
# Will execute both in parallel
#P1.start()
#P2.start()
# Joins threads back to the parent process, which is this
# program
#P1.join()
#P2.join()
#****************** live speech code*************
window = tkinter.Tk()
window.title("Smart Notice Board")
top = Canvas(window,width=400,height=200)
top.pack(fill=X)
button_5 = Button(text='PORTAL SYSTEM', height = 2, width=17, activebackground = '#33B5e5', bg = 'brown', fg = 'white',command = portal )
top.create_window(80,80, anchor='nw', window = button_5)
IN 多处理案例;没有错误,但没有任何效果
【问题讨论】:
-
你没有调用 Tkinter 的 .mainloop()。
-
谢谢,我犯了上面提到的错误;我仍然必须使用 GUI 作为线程和 Livespeech 作为主线程它工作正常;直到我接口 IF cond 以在特定单词上显示按钮,然后它给出错误,即 GUI 不在主线程中。我必须使用语音作为主线程,否则它会给出信号应该在主线程中的错误。另外请查看多处理代码,因为它不起作用并且没有显示错误
标签: python tkinter speech-recognition infinite-loop pocketsphinx