My kivy app closes after a button press if the button callback has a function from another file in it

Viewed 84

The function which is called when the button is pressed

def Button1_Callback(instance):
    show_popup()
    Greet.timex()
    print(f"Button {instance.text} was pressed.")

The popup funtion :

def show_popup():
    layout = GridLayout(cols = 1, padding = 5)
    label = Label(text="executing request")
    layout.add_widget(label)
    pop = Popup(title="Status", content = layout, size_hint = (None, None),  size=(200, 200), auto_dismiss=True)
    pop.open()

and the function which is inside Button1_Callback

def timex():                                                                    # replies with current time
    y = time.strftime("%I %M %P")
    engine.say("The time is " + y + "M")
    engine.runAndWait()

this function is in another file which is name Greet.py

When I run this app, the code works fine but when i click on the button the timex function will execute and the app will stop automatically

also if i comment the line Greet.timex() and run the code then the problem doest occur it only occurs if there is a function inside Button1_Callback

1 Answers

you must run the function in the ui thread (when you press button the function sarted do not start from ui thread) otherwise your app will stop working try that.

from android.runnable import run_on_ui_thread
def Button1_Callback(instance):
    show_popup()
    run_on_ui_thread(Greet.timex)() #here we are making new function that will run on ui thread .
    print(f"Button {instance.text} was pressed.")

Happy coding

Related