title: Program stops using tkinter with pyttsx3, why?
body: <p>I've been googling for almost a week now and I know there are similar questions, but none of them have an answer. So here is one more, which hopefully will lead to an answer. I wrote a small program for my kid where a word shows up, is pronounced and after clearing the word she needs to reproduce it. I used pyttsx3 as a speech module because it's available offline. Program works fine, but just to make it more attractive for her, I decided to build a GUI with tkinter. What happens now is that the window is built, the word shows up is pronounced, but after that the program shuts down without any errors. To check how far the program is executed, I used a print command after every line. The print commands are executed, and are visible in the terminal. Every other line in between, related to tkinter is not executed. I tried to create a different function for the speak engine, same thing happens.</p>
  <p>So again: pyttsx3 works fine, so does tkinter, but together in one program does not. if you remove the line engine.runAndWait() the program executes fine, just without speech.</p>
  <p>Why is this happening and how can I solve it? I am very sorry, please explain it to me like I'm a 40 year old housefather who is trying to learn a new skill without any background in programming or CS, thanks.</p>
  <p>Here is my code, sorry if it's a bit messy, just started a few weeks ago :-)</p>
  <pre><code>import tkinter as tk
  import sys, json, random, pyttsx3
  
  #setup window
  window = tk.Tk()
  window.geometry(&quot;500x500&quot;)
  window.title(&quot;Aliyah's dictee spel&quot;)
  window.configure(background = &quot;black&quot;)
  
  #my photo
  photo1 = tk.PhotoImage(file=&quot;dictee.gif&quot;)
  tk.Label (window, image=photo1, bg=&quot;black&quot;) .grid(row=0, column=0)
  
  def main():
      &quot;&quot;&quot;Setting up the game&quot;&quot;&quot;
      global WORDS_CORRECT, WORDS_WRONG, WORDS
      WORDS_CORRECT = 0
      WORDS_WRONG = 0
      with open('vocabulary.json') as json_file:
          data = json.load(json_file)
          WORDS = data[&quot;words&quot;]
      for widget in window.winfo_children():
          widget.destroy()
      tk.Label (window, image=photo1, bg=&quot;black&quot;) .grid(row=0, column=0)
      tk.Label (window, text = &quot;Dit is het dictee spel voor Aliyah. Wil je beginnen?&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=1)
      tk.Button(window, text=&quot;Ja&quot;, width=6, command=lambda: dictee(WORDS)) .grid(row=2, column=0)
      tk.Button(window, text=&quot;Nee&quot;, width=6, command=sys.exit) .grid(row=2, column=1)
      tk.Button(window, text=&quot;Bewerk lijst&quot;, width=10, command=manage_list) .grid(row=3, column=1)
  
  #def speak(WORD): this did not work, even the last print command in dictee() is executed in terminal
      #engine = pyttsx3.init()
      #nl_voice_id = &quot;com.apple.speech.synthesis.voice.ellen&quot;
      #engine.setProperty('rate', 100)
      #engine.setProperty('voice', nl_voice_id)
      #engine.say(WORD)
      #engine.runAndWait()
  
  
  def dictee(WORDS):
      &quot;&quot;&quot;Show random words and ask for input.&quot;&quot;&quot;
      if WORDS == []:
          for widget in window.winfo_children():
              widget.destroy()
          tk.Label (window, image=photo1, bg=&quot;black&quot;) .grid(row=0, column=0)
          tk.Label (window, text = &quot;Dat waren alle woordjes.&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=1, column=0)
          tk.Label (window, text = &quot;Woordjes goed: %d&quot; % WORDS_CORRECT, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=2, column=0)
          tk.Label (window, text = &quot;Woordjes fout: %d&quot; % WORDS_WRONG, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=3, column=0)
          tk.Label (window, text = &quot;Wil je het nog een keer proberen?&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=4, column=0)
          tk.Button(window, text=&quot;Ja&quot;, width=6, command=main) .grid(row=5, column=0)
          tk.Button(window, text=&quot;Nee&quot;, width=6, command=exit_game) .grid(row=5, column=1)
          window.update()
      else:
          random.shuffle(WORDS)
          for widget in window.winfo_children():
              widget.destroy()
          WORD = WORDS[0]
          tk.Label (window, image=photo1, bg=&quot;black&quot;) .grid(row=0, column=0)
          word = tk.Label (window, text = WORD, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 20 bold&quot;)
          word.grid(row=1, column=0)
          window.update()
          #speak(WORD)this is related to speak()
          engine = pyttsx3.init()
          nl_voice_id = &quot;com.apple.speech.synthesis.voice.ellen&quot;
          engine.setProperty('rate', 100)
          engine.setProperty('voice', nl_voice_id)
          engine.say(WORD) #WORD is being said!
          engine.runAndWait()#&lt;&lt; if you remove this line, the program is executed fine, but without speech
          #engine.stop() this did not work
          window.after(3000)
          print('after after')#all print commands after this are executed in terminal, all other lines are not executed
          word['text'] = ''
          print('after clear text')
          window.update()
          print('after update')
          ANSWER = tk.Entry(window, width=20, bg=&quot;white&quot;)
          print('after creating entry box')
          ANSWER.grid(row=2, column=0)
          print('after positioning entry window')
          tk.Button(window, text=&quot;Check&quot;, width=6, command=lambda: check(ANSWER,WORD)) .grid(row=3, column=0)
          print('after creating button')
          WORDS.remove(WORD)
          print('after words.remove')
  
  def check(ANSWER, WORD):
      '''Cross check word shown with answer given'''
      global WORDS_CORRECT, WORDS_WRONG
      if ANSWER.get() == WORD:
          tk.Label (window, text = &quot;Wat goed!&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=4, column=0)
          WORDS_CORRECT += 1
      else:
          tk.Label (window, text = &quot;Jammer!&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=4, column=0)
          WORDS_WRONG += 1
  
      tk.Label (window, text = &quot;Wil je het nog een keer proberen?&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=5, column=0)
      tk.Button(window, text=&quot;Ja&quot;, width=6, command=lambda: dictee(WORDS)) .grid(row=6, column=0)
      tk.Button(window, text=&quot;Nee&quot;, width=6, command=exit_game) .grid(row=6, column=1)
  
  
  def manage_list():
      '''manage the list of words by user'''
      with open('vocabulary.json', &quot;r&quot;) as f:
          data = json.load(f)
          WORDS = data[&quot;words&quot;]
      for widget in window.winfo_children():
          widget.destroy()
      window.update()
      tk.Label (window, text = &quot;Dit zijn alle woorden in de vocabulaire&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=0, column=0)
      tk.Label (window, text = WORDS, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=1, column=0)
      tk.Label (window, text = &quot;Wil je woorden toevoegen of verwijderen?&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=2, column=0)
      tk.Button(window, text=&quot;alles verwijderen&quot;, command=lambda: clear_words('vocabulary.json')) .grid(row=5, column=0)
      words_to_add = tk.Entry(window, width=40, bg=&quot;white&quot;)
      words_to_add.grid(row=3, column=0)
      tk.Button(window, text=&quot;toevoegen&quot;, width=6, command=lambda: add_words('vocabulary.json', words_to_add.get())) .grid(row=8, column=2)
      tk.Button(window, text=&quot;hoofdmenu&quot;, width=6, command=main) .grid(row=10, column=0)
  
  def add_words(filename, wordt_text):
      with open(filename, &quot;r+&quot;) as f:
          data = json.load(f)
          data[&quot;words&quot;].extend(wordt_text.split())
          f.seek(0)
          json.dump(data, f, indent=4)
      manage_list()
  
  def clear_words(filename):
      with open('vocabulary.json', &quot;w+&quot;) as f:
          data = {&quot;words&quot;:[]}
          json.dump(data, f, indent=4)
      manage_list()
  
  def exit_game():
      '''summarize results and exit after pushing enter'''
      for widget in window.winfo_children():
          widget.destroy()
      tk.Label (window, image=photo1, bg=&quot;black&quot;) .grid(row=0, column=0)
      tk.Label (window, text = &quot;Tot de volgende keer.&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=1, column=0)
      tk.Label (window, text = &quot;Woordjes goed: %d&quot; % WORDS_CORRECT, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=2, column=0)
      tk.Label (window, text = &quot;Woordjes fout: %d&quot; % WORDS_WRONG, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=3, column=0)
      tk.Label (window, text = &quot;Klik op OK om af te sluiten&quot;, bg=&quot;black&quot;, fg=&quot;white&quot;, font=&quot;none 12 bold&quot;) .grid(row=4, column=0)
      tk.Button(window, text=&quot;OK&quot;, width=6, command=sys.exit) .grid(row=5, column=0)
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  
  window.mainloop()
  </code></pre>
  <p>Here is the vocabulary.json file:</p>
  <pre><code>{
      &quot;words&quot;: [
          &quot;huis&quot;,
          &quot;muis&quot;,
          &quot;bal&quot;,
          &quot;vuur&quot;,
          &quot;muur&quot;,
          &quot;ik&quot;,
          &quot;zon&quot;,
          &quot;hok&quot;,
          &quot;poep&quot;,
          &quot;aap&quot;,
          &quot;noot&quot;,
          &quot;hij&quot;,
          &quot;zij&quot;,
          &quot;test&quot;,
          &quot;woord&quot;
      ]
  }
  </code></pre>
  <p>here is a link to the image I used: <a href="https://www.obsbeekbergen.nl/wp-content/uploads/sites/34/2016/04/dictee.gif" rel="nofollow noreferrer">https://www.obsbeekbergen.nl/wp-content/uploads/sites/34/2016/04/dictee.gif</a></p>
  <p>The output in the terminal when the program is shut down (again, no errors):</p>
  <pre><code>after clear text
  after update
  after creating entry box
  after positioning entry window
  after creating button
  after words.remove 
  </code></pre>
  <p>I'm using python 3.8.5 with MacOs Mojave 10.14.6.</p>