title: Voice assistant with kivy GUI
body: <p>Hello everyone I'm working on a voice assistant project using python and I'd like to develop a kivy GUI for the assistant with just two buttons start and exit .. I do the buttons but i had troubles to connect the buttons with the assistant script I'm too amateur on kivy , Can any why help please l'll be so thankfull
  these are my files :
  main.py (for the assistant script) :</p>
  <pre><code>import speech_recognition as sr  # recognise speech
  import playsound  # to play an audio file
  from gtts import gTTS  # google text to speech
  import random
  from time import ctime  # get time details
  import webbrowser  # open browser
  import ssl
  import certifi
  import time
  import os  # to remove created audio files
  from PIL import Image
  import subprocess
  import pyautogui  # screenshot
  import pyttsx3
  import bs4 as bs
  import urllib.request
  
  
  class person:
      name = ''
  
      def setName(self, name):
          self.name = name
  
  
  class asis:
      name = ''
  
      def setName(self, name):
          self.name = name
  
  
  def there_exists(terms):
      for term in terms:
          if term in voice_data:
              return True
  
  
  def engine_speak(text):
      text = str(text)
      engine.say(text)
      engine.runAndWait()
  
  
  r = sr.Recognizer()  # initialise a recogniser
  
  
  # listen for audio and convert it to text:
  def record_audio(ask=&quot;&quot;):
      with sr.Microphone() as source:  # microphone as source
          if ask:
              engine_speak(ask)
          audio = r.listen(source, 5, 5)  # listen for the audio via source
          print(&quot;Done Listening&quot;)
          voice_data = ''
          try:
              voice_data = r.recognize_google(audio)  # convert audio to text
          except sr.UnknownValueError:  # error: recognizer does not understand
              engine_speak('I did not get that')
          except sr.RequestError:
              engine_speak('Sorry, the service is down')  # error: recognizer is not connected
          print(&quot;&gt;&gt;&quot;, voice_data.lower())  # print what user said
          return voice_data.lower()
  
  
  # get string and make a audio file to be played
  def engine_speak(audio_string):
      audio_string = str(audio_string)
      tts = gTTS(text=audio_string, lang='en')  # text to speech(voice)
      r = random.randint(1, 20000000)
      audio_file = 'audio' + str(r) + '.mp3'
      tts.save(audio_file)  # save as mp3
      playsound.playsound(audio_file)  # play the audio file
      print(asis_obj.name + &quot;:&quot;, audio_string)  # print what app said
      os.remove(audio_file)  # remove audio file
  
  
  def respond(voice_data):
      # 1: greeting
      if there_exists(['hey', 'hi', 'hello']):
          greetings = [&quot;hey, how can I help you&quot; + person_obj.name, &quot;hey, what's up?&quot; + person_obj.name,
                       &quot;I'm listening&quot; + person_obj.name, &quot;how can I help you?&quot; + person_obj.name,
                       &quot;hello&quot; + person_obj.name]
          greet = greetings[random.randint(0, len(greetings) - 1)]
          engine_speak(greet)
  
      # 2: name
      if there_exists([&quot;what is your name&quot;, &quot;what's your name&quot;, &quot;tell me your name&quot;]):
          if person_obj.name:
              engine_speak(&quot;whats with my name &quot;)
          else:
              engine_speak(&quot;my name is Jexi . what's your name?&quot;)
  
      if there_exists([&quot;my name is&quot;]):
          person_name = voice_data.split(&quot;is&quot;)[-1].strip()
          engine_speak(&quot;okay, i will remember that &quot; + person_name)
          person_obj.setName(person_name)  # remember name in person object
  
      if there_exists([&quot;your name should be&quot;]):
          asis_name = voice_data.split(&quot;be&quot;)[-1].strip()
          engine_speak(&quot;okay, i will remember that my name is &quot; + asis_name)
          asis_obj.setName(asis_name)  # remember name in asis object
  
      
  
      # 3: greeting
      if there_exists([&quot;how are you&quot;, &quot;how are you doing&quot;]):
          engine_speak(&quot;I'm very well, thanks for asking &quot; + person_obj.name)
  
      # 4: time
      if there_exists([&quot;what's the time&quot;, &quot;tell me the time&quot;, &quot;what time is it&quot;]):
          time = ctime().split(&quot; &quot;)[3].split(&quot;:&quot;)[0:2]
          if time[0] == &quot;00&quot;:
              hours = '12'
          else:
              hours = time[0]
          minutes = time[1]
          time = hours + &quot; hours and &quot; + minutes + &quot;minutes&quot;
          engine_speak(time)
  
      # 5: search google
      if there_exists([&quot;search for&quot;]) and 'youtube' not in voice_data:
          search_term = voice_data.split(&quot;for&quot;)[-1]
          url = &quot;https://google.com/search?q=&quot; + search_term
          webbrowser.get().open(url)
          engine_speak(&quot;Here is what I found for&quot; + search_term + &quot;on google&quot;)
  
      # 6: search youtube
      if there_exists([&quot;youtube&quot;]):
          search_term = voice_data.split(&quot;for&quot;)[-1]
          url = &quot;https://www.youtube.com/results?search_query=&quot; + search_term
          webbrowser.get().open(url)
          engine_speak(&quot;Here is what I found for &quot; + search_term + &quot;on youtube&quot;)
  
      # 7: get stock price
      if there_exists([&quot;price of&quot;]):
          search_term = voice_data.split(&quot;for&quot;)[-1]
          url = &quot;https://google.com/search?q=&quot; + search_term
          webbrowser.get().open(url)
          engine_speak(&quot;Here is what I found for &quot; + search_term + &quot; on google&quot;)
  
      # 8 time table
      if there_exists([&quot;show my time table&quot;]):
          im = Image.open(r&quot;D:\WhatsApp Image 2019-12-26 at 10.51.10 AM.jpeg&quot;)
          im.show()
  
      # 9 weather
      if there_exists([&quot;weather&quot;]):
          search_term = voice_data.split(&quot;for&quot;)[-1]
          url = &quot;https://www.google.com/search?sxsrf=ACYBGNSQwMLDByBwdVFIUCbQqya-ET7AAA%3A1578847393212&amp;ei=oUwbXtbXDN-C4-EP-5u82AE&amp;q=weather&amp;oq=weather&amp;gs_l=psy-ab.3..35i39i285i70i256j0i67l4j0i131i67j0i131j0i67l2j0.1630.4591..5475...1.2..2.322.1659.9j5j0j1......0....1..gws-wiz.....10..0i71j35i39j35i362i39._5eSPD47bv8&amp;ved=0ahUKEwiWrJvwwP7mAhVfwTgGHfsNDxsQ4dUDCAs&amp;uact=5&quot;
          webbrowser.get().open(url)
          engine_speak(&quot;Here is what I found for on google&quot;)
  
      # 10 stone paper scisorrs
      if there_exists([&quot;game&quot;]):
          voice_data = record_audio(&quot;choose among rock paper or scissor&quot;)
          moves = [&quot;rock&quot;, &quot;paper&quot;, &quot;scissor&quot;]
  
          cmove = random.choice(moves)
          pmove = voice_data
  
          engine_speak(&quot;The computer chose &quot; + cmove)
          engine_speak(&quot;You chose &quot; + pmove)
          # engine_speak(&quot;hi&quot;)
          if pmove == cmove:
              engine_speak(&quot;the match is draw&quot;)
          elif pmove == &quot;rock&quot; and cmove == &quot;scissor&quot;:
              engine_speak(&quot;Player wins&quot;)
          elif pmove == &quot;rock&quot; and cmove == &quot;paper&quot;:
              engine_speak(&quot;Computer wins&quot;)
          elif pmove == &quot;paper&quot; and cmove == &quot;rock&quot;:
              engine_speak(&quot;Player wins&quot;)
          elif pmove == &quot;paper&quot; and cmove == &quot;scissor&quot;:
              engine_speak(&quot;Computer wins&quot;)
          elif pmove == &quot;scissor&quot; and cmove == &quot;paper&quot;:
              engine_speak(&quot;Player wins&quot;)
          elif pmove == &quot;scissor&quot; and cmove == &quot;rock&quot;:
              engine_speak(&quot;Computer wins&quot;)
  
      # 11 toss a coin
      if there_exists([&quot;toss&quot;, &quot;flip&quot;, &quot;coin&quot;]):
          moves = [&quot;head&quot;, &quot;tails&quot;]
          cmove = random.choice(moves)
          engine_speak(&quot;The computer chose &quot; + cmove)
  
      # 12 calc
      if there_exists([&quot;plus&quot;, &quot;minus&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, &quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;]):
          opr = voice_data.split()[1]
  
          if opr == '+':
              engine_speak(int(voice_data.split()[0]) + int(voice_data.split()[2]))
          elif opr == '-':
              engine_speak(int(voice_data.split()[0]) - int(voice_data.split()[2]))
          elif opr == 'multiply':
              engine_speak(int(voice_data.split()[0]) * int(voice_data.split()[2]))
          elif opr == 'divide':
              engine_speak(int(voice_data.split()[0]) / int(voice_data.split()[2]))
          elif opr == 'power':
              engine_speak(int(voice_data.split()[0]) ** int(voice_data.split()[2]))
          else:
              engine_speak(&quot;Wrong Operator&quot;)
  
      # 13 screenshot
      if there_exists([&quot;capture&quot;, &quot;my screen&quot;, &quot;screenshot&quot;]):
          myScreenshot = pyautogui.screenshot()
          myScreenshot.save('D:/screenshot/screen.png')
  
          # 14 to search wikipedia for definition
      if there_exists([&quot;definition of&quot;]):
          definition = record_audio(&quot;what do you need the definition of&quot;)
          url = urllib.request.urlopen('https://en.wikipedia.org/wiki/' + definition)
          soup = bs.BeautifulSoup(url, 'lxml')
          definitions = []
          for paragraph in soup.find_all('p'):
              definitions.append(str(paragraph.text))
          if definitions:
              if definitions[0]:
                  engine_speak('im sorry i could not find that definition, please try a web search')
              elif definitions[1]:
                  engine_speak('here is what i found ' + definitions[1])
              else:
                  engine_speak('Here is what i found ' + definitions[2])
          else:
              engine_speak(&quot;im sorry i could not find the definition for &quot; + definition)
  
      if there_exists([&quot;exit&quot;, &quot;quit&quot;, &quot;goodbye&quot;]):
          engine_speak(&quot;bye&quot;)
          exit()
  
  
  time.sleep(1)
  
  person_obj = person()
  asis_obj = asis()
  asis_obj.name = 'kiki'
  engine = pyttsx3.init()
  
  while (1):
      voice_data = record_audio(&quot;Recording&quot;)  # get the voice input
      print(&quot;Done&quot;)
      print(&quot;Q:&quot;, voice_data)
      respond(voice_data)  # respond
  
  
  </code></pre>
  <p>kv.py (for the GUI) :</p>
  <pre><code>
  
  from kivy import Config
  Config.set('graphics', 'multisamples', '0')
  Config.set('graphics', 'width', '360')
  Config.set('graphics', 'height', '600')
  import os
  os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
  import kivy
  kivy.require('1.9.0')
  from kivy.app import App
  from kivy.uix.label import Label
  from kivy.uix.gridlayout import GridLayout
  from kivy.uix.textinput import TextInput
  from kivy.uix.button import Button
  from kivy.uix.widget import Widget
  from kivy.uix.boxlayout import BoxLayout
  from kivy.core.window import Window
  
  import random
  from kivy.uix.boxlayout import BoxLayout
  from kivy.uix.textinput import TextInput
  
  # Window.clearcolor = (237, 235, 235, 235)
  # create a background class which inherits the boxlayout class 
  class Background(BoxLayout): 
      def __init__(self, **kwargs): 
          super().__init__(**kwargs) 
      pass
    
  # Create App class with name of your app 
  class SampleApp(App): 
    
  # return the Window having the background template. 
      def build(self): 
          return Background() 
  
    
  # run app in the main function 
  if __name__ == '__main__': 
      SampleApp().run() 
  
  </code></pre>
  <p>and sample.kv file :</p>
  <pre><code>#:kivy 1.0
  &lt;Background&gt;: 
      id: main_win 
      orientation: &quot;vertical&quot;
      spacing: 10
      space_x: self.size[0]/3
  
  
      canvas.before: 
          Color: 
              rgba: (1, 1, 1, 1) 
          Rectangle: 
              source:'back1.jpg'
              size: root.width, root.height 
              pos: self.pos 
  
  
      Button: 
          text: &quot; Start &quot;
          pos_hint :{'center_x':0.5, 'center_y':0} 
          size_hint: .26,0
          background_color:(1, 1, 1, 1)
          on_press:
              #
  
  
  
  
      Button: 
          text: &quot; exit &quot;
          pos_hint :{'center_x':0.5, 'center_y':0} 
          size_hint: .26,0
          background_color:(1, 1, 1, 1)
          on_press:
              #
      
  </code></pre>