I have one problem with the following code:
- from win32com.client import constants
-
import win32com.client
-
import win32gui
-
import pythoncom
-
from Tkinter import *
-
-
class SpeechRecognition:
-
def __init__(self, wordsToAdd):
-
self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
-
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
-
self.context = self.listener.CreateRecoContext()
-
self.grammar = self.context.CreateGrammar()
-
self.grammar.DictationSetState(0)
-
self.wordsRule = self.grammar.Rules.Add("wordsRule",constants.SRATopLevel + constants.SRADynamic, 0)
-
self.wordsRule.Clear()
-
[ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]
-
self.grammar.Rules.Commit()
-
self.grammar.CmdSetRuleState("wordsRule", 1)
-
self.grammar.Rules.Commit()
-
self.eventHandler = ContextEvents(self.context)
-
def say(self, phrase):
-
self.speaker.Speak(phrase)
-
-
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
-
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
-
newResult = win32com.client.Dispatch(Result)
-
globals()['fraza']=newResult.PhraseInfo.GetText()
-
print "You said: ",fraza
-
if fraza=='winamp':
-
speechReco.say("What")
-
graf.list1.delete(0,END)
-
graf.list1.insert(END,fraza)
-
-
class App:
-
-
def __init__(self, master):
-
self.list1=Listbox(master,bg='pink',width='20',height='1',font=("Comic Sans MS",14))
-
self.list1.pack(anchor=W)
-
self.list1.insert(0,'Hey')
-
-
-
if __name__=='__main__':
-
globals()['fraza']=1
-
wordsToAdd = ['winamp','play']
-
speechReco = SpeechRecognition(wordsToAdd)
-
root=Tk()
-
graf=App(root)
-
root.mainloop()
-
Problem is with these 2 lines (41,42):
graf.list1.delete(0,END)
graf.list1.insert(END,fraza)
because program stops and is not responding any more when it come to this lines. If these two lines are not in class ContextEvents, the program works fine but i need them in there. Even if I call function with this two lines from ContextEvents the program freeze.
The program must delete text and insert text 'winamp' in Tkinter Label when i say winamp in microfone. Everything works fine but when i say 'winamp' program freeze. How can i solve this problem. Please help.