Function inside function starts before any command in kivy and python

I created a simple app to do some stuff with API. This function “kuramac” takes about 2-3 seconds to finish and while it runs, I’d like to disable a button that prevents users to click it several times. Here is my .py code:

class WidgetsExample(BoxLayout):  
    # get objects properties from kivy file
    my_text = StringProperty("")
    adress = ObjectProperty(None)
    name = ObjectProperty(None)      
    myBtn = ObjectProperty(None)
    
# function assing to a button
def kuramac(self):
    
    # this should disable the button while function is running
    self.myBtn.disabled = True
    
    inputAddress = self.adress.text
    inputName = self.name.text
    
    # main function posting and object and into a sonicwall 
    response = postIpv4Object(inputName, inputAddress)
    self.my_text = response['message'] + 'n' # + response['comitResponse']

    # this should enable the button while function is running
    self.myBtn.disabled = False


class SonciWallApp(App):
    pass

SonciWallApp().run()

If I leave only self.myBtn.disabled = True it disables a button after the postIpv4Object function finish. And if I add at the end self.myBtn.disabled = False, basically it does nothing. Buttons stay as if nothing happened. I tried to use sleep.time() but it didn’t do much.

Here is .kv code:

<WidgetsExample@BoxLayout>:
    adress:adress
    name:name
    myBtn:myBtn
    orientation: 'vertical'
    color: 1,1,1,1
    Label:
        text:'Dodanie do grupy na sonicu'
        size_hint: None,.5
        height: '10dp'
        pos_hint:{ "center_x": .5}
    GridLayout:
        cols: 2
        rows: 2
        Label:
            text:'Adres:'
        TextInput:
            text:''
            id: adress
        Label:
            text:'Nazwa:'
        TextInput:
            text:''
            id: name

    Button:
        id: myBtn
        text: 'Wyślij'
        on_press: root.kuramac()
        disabled: root.isEnabled

    Label:
        text: root.my_text

Answer

The problem is that you are running kuramac() on the main thread. Kivy uses the main thread to update the GUI, but cannot do the updates if you are holding the main thread. To fix that, just run kuramac() on another thread. For example, just redefine some methods:

def kuramac(self):
    Thread(target=self.real_kuramac, daemon=True).start()

def real_kuramac(self):
    # this should disable the button while function is running
    self.myBtn.disabled = True

    inputAddress = self.adress.text
    inputName = self.name.text

    # main function posting and object and into a sonicwall
    response = postIpv4Object(inputName, inputAddress)
    self.my_text = response['message'] + 'n'  # + response['comitResponse']

    # this should enable the button while function is running
    self.myBtn.disabled = False