1

I'm currently just testing an idea for something simple, using the idea of a workout program as an example. I'm very new to python and so this is more of a test than anything. What I want to have happen is open a window with the workout (just a tkinter label) but then passively in the background have a speech recognition command run to catch for the person saying "next". I'm not interested in having people tell me how to make other bits more effcient because I'm aware it's bad.

I'd just like a solution to make the command SpeechRecognition1 run passively whilst the "5 pressups" label is displayed. Is there anyway to do this?

from tkinter import * import sys import speech_recognition as sr def NextWorkout1(): workout1.destroy() Workout2() def Workout1(): global workout1 workout1 = Tk() workout1.geometry("300x44") workout1.configure(background="lightblue") workout1.resizable(0,0) workout1.title("Pressups") insLabel = Label(workout1, text="5 pressups", fg="red", bg="lightblue", font="Arial 25 bold") insLabel.pack() workout1.mainloop() def Workout2(): global workout2 workout2 = Tk() workout2.geometry("300x50") workout2.configure(background="lightblue") workout2.resizable(0,0) workout2.title("Starjumps") insLabel = Label(workout2, text="15 starjumps", fg="red", bg="lightblue", font="Arial 25 bold") insLabel.pack() workout2.mainloop() def SpeechRecognition1(): r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source) for i in range(1): command = (r.recognize_google(audio)) if command == "next": NextWorkout1() else: print("hi") def SpeechRecognition2(): global WorkoutNumber WorkoutNumber = 0 r = sr.Recognizer() with sr.Microphone() as source: audio = r.listen(source) for i in range(1): command = (r.recognize_google(audio)) if command == "next": NextWorkout2() else: print("hi") Workout1() 
1
  • do you know about threads? Commented May 14, 2018 at 19:34

1 Answer 1

4

use the after_idle function with your function as the callback function

in this order

from threading import Thread workout1 = Tk() thread = Thread(target = SpeechRecognition1) workout1.after_idle(thread.start) workout1.mainloop() 
Sign up to request clarification or add additional context in comments.

8 Comments

I added that and now it simply doesn't open the window and instead just goes straight to do SpeechRecognition1(). gyazo.com/0017cba5b8145d707a4b13fcbc469697
use a threaded method, otherwise it will get stuck on line 2 stackoverflow.com/a/2906014/6471494
I have edited the answer to contain the threading part. please mark the question complete if its sufficient.
it says that “thread” has now been referenced before assignment?
Okay, thank you so much! I've got my head around the concept of threads now and should be able to work from here! I'll mark this question, thanks a lot!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.