Search code examples
pythonpython-3.xwhile-loopkivykivy-language

Python Kivy 2 Buttons 2 Different Actions in Same Layout


I develop voice assistant with python and ı want to add basic gui with kivyy. I am writing basic gui with kivy and ı want to learn kivy framework so that ı am trying to improve myself with creating GUIs. But in this project ı have an error. Firstly, let me put my code here.

class Assistant(App):

def build(self):
    rl = RelativeLayout()



    b1 = Button(size_hint=(.2, .2),
                pos_hint={'x': 0, 'top': 1},
                text="Click For Exit!",
                color=(1,0, .15, 1))

    b2 = Button(size_hint=(.2, .2),
                pos_hint={'right': 1, 'top': 1},
                text="Run Program!",
                color=(8,1, .56, 4))


    image = Image(source="background_img.jpg")
    b2.bind(on_press=self.RunProgram)
    b1.bind(on_press=self.CloseProgram)


    textinput = TextInput(font_size=30,
                          size_hint_y=None,
                          height=100,
                          multiline=False,
                          allow_copy=True,
                          auto_indent=True)

    textinput.bind(text=self.on_text)
    textinput.bind(on_text_validate=self.on_enter)

    rl.add_widget(image)
    rl.add_widget(b1)
    rl.add_widget(textinput)
    rl.add_widget(b2)

and for this two button there is their functions.

    def CloseProgram(self, event):
        sys.exit()

    def RunProgram(self, event): 
        while True:
              jarvis.run(listen())

As you can see ı have 2 button , one is quit from program and the other one is listen users. Also ı have text input that user can input something to give command from keyboard to assistant. But unfortunately when ı click "Run Program" button, Everything is good at first after when ı want to type something or if ı want to click "Exit Program" my GUI stop working. ( everything freezes and crashes ) but in the background my assistant continues to listen to user (me). How can ı solve this problem ?

İn this situation when ı delete while loop everything goes fine but when user wants to talk for assistant have to click the "Run Program" button every time. Here ı want user can click buttons or can type to text input when program working on background. Thanks in advance.

More information:

I use windows10 , and python version 3.8.1


Solution

  • The problem is solved using python thread. Thanks everyone.