Lakshmi Vallabh
Lakshmi Vallabh

Reputation: 124

How can I change the on_release functionality of a KivyMD button using Python code in KivyMD?

How can we change the on_release function of KivyMD button using Python code? I have added a print statement to the KivyMD button initially and I have changed on_release function of KivyMD button with Python code but the problem is, it joins my new function to the button instead of replacing its previous function. That mean, it also executes the previous function assigned to the button but I want to change function completely like replacing it with a new function using Python code.

Here is my Python code

self.root.ids.btn.on_release = self.new_function

Thanks in advance :)

Upvotes: 0

Views: 500

Answers (1)

John Anderson
John Anderson

Reputation: 39117

If your initial on_release function is set in kv, then I don't think you can remove or replace it. I believe the fix is to extend the Button class and define the initial on_release function in its __init__() method. Like this:

class MyButt(Button):
    def __init__(self, **kwargs):
        self.on_release = partial(print, 'hi')
        return super(MyButt, self).__init__(**kwargs)

Then replace that Button in your kv with MyButt, and remove the on_release: line fom the kv.

Upvotes: 1

Related Questions