'Why does the button print twice? (Kivy Python)

I'm trying to make a button and I'm doing this print as a test, I'm not why it prints twice? I'm new to Kivy so I'm not too sure on what I'm doing, any help is appreciated thank you.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button


def bannedBooksMain(self, instance):
    print('The button test is being pressed')

class mainApp(GridLayout):

    def __init__(self, **kwargs):
        super(mainApp, self).__init__(**kwargs)
        self.cols = 2
        btn1 = Button(text='Home Page')
        btn1.bind(state=bannedBooksMain)
        self.add_widget(btn1)


class MyApp(App):

    def build(self):
        return mainApp()
MyApp().run()


Solution 1:[1]

btn1.bind(state=bannedBooksMain)

This (state=) will initiate a callback whenever the state property changes, this happens for both button press and release, which is why you're seeing it twice

You should use on_press= or on_release= instead, depending on when you want the action fired.

Personally, I prefer the on_release= combined with always_release being false. That way, if I inadvertently press the button, I can move the mouse point away from the button before releasing, and there will be no callback.

See here for further detail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1