0

I have to do the threading in 1 of 2 codes; one is of speech to text conversion and other is of Tkinter GUI and they both are interrelated that if one says something then it has to perform some changes in GUI. Problem is that when I make thread GUI and speech in mainloop, it works fine until I execute the condition that if someone says "HOME" u have to show new button in GUI; it gives an error that MAIN THREAD is not in main loop And when I try to keep Speech code in thread and GUI in mainloop; it gives an error that "SIGNAL should be in mainloop" If I keep both in the main loop and don't do threading then if GUI is written first then it shows GUI and stuck there and if speech is written first it keeps running the speech code(since it is infinity FOR loop)

#*********************************** 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()
window = tkinter.Tk()
window.title("Smart Notice Board")

top = Canvas(window,width=400,height=200)
top.pack(fill=X)

#*************** TKINTER GUI CODE******************
def gui():

 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)
 window.mainloop()
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*************
    #gui()
    #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)

        button_6= Button(text='HOME SYSTEM', height = 2, width=17, activebackground = '#33B5e5', bg = 'brown', fg = 'white',command  = portal )
        top.create_window(20,20, anchor='nw', window = button_6)

The code works fine until I put if cond,

Code works fine until i put if cond

Shows this error,

show this error

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87

0 Answers0