Hello, I have this code where if I press the button and there is some processing happening, the app starts to crash if I do anything, like pressing the button and then starting to type numbers in the text field. How can I fix this?
import time
from kivy.lang import Builder
import requests
from kivymd.app import MDApp
class Example(MDApp):
def build(self):
# Define the KV layout as a string
self.kv = """
MDFloatLayout:
MDCard:
orientation: "vertical"
size_hint: None, None
size: "350dp", "300dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
padding: 25
spacing: 25
radius: [20, 20, 20, 20]
# Mobile Number Input Field
MDTextField:
mode: "round"
id: mobile_number
hint_text: "9xxxxxxxxx"
helper_text: "Must be 10-digit only"
helper_text_mode: "on_focus"
icon_right: "cellphone"
input_type: "number"
max_text_length: 10
size_hint_x: 1
# Login Button
MDFillRoundFlatButton:
text: "Login"
size_hint_x: 0.5
pos_hint: {"center_x": 0.5}
on_release: app.send_mobile_number()
"""
return Builder.load_string(self.kv)
def send_mobile_number(self):
mobile_number = self.root.ids.mobile_number.text
api_url = "api_here"
time.sleep(5)
payload = {"mobile_number": mobile_number}
# Send POST request
try:
# print(response.json())
print("Mobile Number:", mobile_number)
except requests.exceptions.RequestException as e:
print("Error:", e)
def on_leave(self):
self.root.ids.mobile_number.text = ""
# Run the app
Example().run()