【问题标题】:Tkinter with another infinite loop ( For speech to text conversion)带有另一个无限循环的 Tkinter(用于语音到文本的转换)
【发布时间】: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


【解决方案1】:

这是我用来从队列中获取鼠标移动事件的光标位置并采取相应措施的代码:

def check_mouse(self):
    while True:
        item = self.mouse.get_item()
        if item is None:
            break
        else:
            self.master.after_idle(self.mouse_move, *item)

    self.master.after(INTERVAL, self.check_mouse)

这是第一次在 mainloop 调用之前调用,还有另一个 self.master.after(INTERVAL, self.check_mouse)

所以让你的 Tkinter GUI 在主循环中完成它的工作,你应该创建另一个循环,其中的任务将在 INTERVAL(以毫秒为单位)之后运行,并且它会在其工作完成后的每个 INTERVAL 周期调用自己。

【讨论】:

  • 哦,我明白了,所以我可以在主循环中制作 GUI,但无法弄清楚如何在另一个循环中将语音写入文本代码;我必须做类似的工作,因为当某些短语被捕获然后它有做一些任务,否则它没有工作。此外,无论我在哪里调用 tkinter mainloop 代码都会冻结在那里,当我关闭它时,它会进入下一行。你能帮我编写语音到文本循环的代码吗?
  • 我猜你可以用你的 for 循环而不是 while True 在这里尝试这个逻辑。
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 2015-11-07
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多