Bind a button already on_press
<p>I want to change the function which is launched when my button is pressed.</p>

<p>Python file:</p>

<pre><code>class UpdateScreen(Screen):

swimbot = {}
def swimbot_connected(self):
    wallouh = list(AdbCommands.Devices())
    if not wallouh:
        self.ids['text_label'].text = 'Plug your Swimbot and try again'
    else:
        for devices in AdbCommands.Devices():
            output = devices.serial_number
            if re.match("^(.*)swimbot", output):
                self.ids['mylabel'].text = 'Etape 2: Do you need an update ?'
                self.ids['action_button'].text = 'Check'
                self.ids['action_button'].bind(on_press = self.check_need_update())
            else:
                self.ids['text_label'].text = 'Plug your Swimbot and try again'
</code></pre>

<p>Kv file :</p>

<pre><code>&lt;UpdateScreen&gt;:
BoxLayout:
    id: update_screen_layout
    orientation: 'vertical'
    Label:
        id: mylabel
        text: "Etape 1: Connect your Swimbot"
        font_size: 26
    Label:
        id: text_label
        text: "Truc"
        font_size: 24
    FloatLayout:
        size: root.size
        pos: root.pos
        Button:
            id: action_button
            pos_hint: {'x': .05, 'y':.25}
            size_hint: (.9, .4)
            text: "Try"
            font_size: 24
            on_press: root.swimbot_connected()
</code></pre>

<p>But i think It's not the right way to do that with this :</p>

<pre><code>self.ids['action_button'].bind(on_press = self.check_need_update())
</code></pre>

<p>With that I go directly to check_need_update(), it doesn't wait I press the button.</p>

<p>When you put <code>()</code> after a function in python you execute that function. So when you type</p>

<pre><code>self.ids['action_button'].bind(on_press = self.check_need_update())
</code></pre>

<p>You will actually pass the result of self.check_needed_update to <code>on_press</code>. So you need:</p>

<pre><code>self.ids['action_button'].bind(on_press = self.check_need_update)
</code></pre>

<p>This is different in kv language. There you actually need to put <code>()</code> when binding a function. You can also put arguments there that will get evaluated when the callback is called (and not when the definition get read).</p>

<p>But the python code will actually not do what you want. It will bind an additional function to that button, but it will not overwrite the other function. You can unbind callbacks but it's a bit complicated (see <a href="https://kivy.org/docs/api-kivy.event.html#kivy.event.EventDispatcher.unbind" rel="nofollow">the documentation here</a>).</p>

<p>Instead I would change the called function in a way that it behaves differently:</p>

<pre><code>class UpdateScreen(Screen):

    state = 0
    swimbot = {}
    def swimbot_connected(self):
        if state == 0:
            self._original_swimbot_connected()
        if state == 1:
            self.check_need_update
</code></pre>

<p>And then instead of unbinding and binding a new function to the button you can just modify <code>UpdateScreen.state</code>.</p>

python button kivy bind