GCIreland
GCIreland

Reputation: 155

How to trigger a function when KivyMD Speed Dial Button pressed?

I am trying to get a function to trigger when a button on The KivyMD Speed Dial is pressed. I am currently using a callback from the .kv code as seen here:

MDFloatingActionButtonSpeedDial:
        bg_hint_color: app.theme_cls.primary_light
        data: app.data
        root_button_anim: True
        callback: app.btn

This then call the python code:

#DATA FOR THE SPEED DIAL
data = {
        'Create': 'file-document',
        'Open': 'folder-open'
        
    }
#FUNCTION HERE
    def btn(self, button):
        print(button)
        if button =="<kivymd.uix.button.MDFloatingBottomButton object at 0x000001BBCAF5BA50>":
            print("test")

I enter the argument button to get the name of the button so I put that in a if statement to see if it is all working so far but the text is not being printed. The button is being printed so that's why I put it in the if statement put text is not being printed so I really don't know what is going on here. Hope the community can help.

Upvotes: 0

Views: 668

Answers (1)

Ne1zvestnyj
Ne1zvestnyj

Reputation: 1397

isinstance checks which class the object belongs to.

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingBottomButton

KV = '''
MDScreen:

    MDFloatingActionButtonSpeedDial:
        data: app.data
        root_button_anim: True
        callback: app.callback
'''


class Example(MDApp):
    data = {
        'Create': 'file-document',
        'Open': 'folder-open',
    }

    def build(self):
        return Builder.load_string(KV)

    def callback(self, instance):
        print('callback')
        icon = instance.icon
        # if you want check button, use
        if isinstance(instance, MDFloatingBottomButton):
            if icon == 'file-document':
                print('Read file')
            elif icon == 'folder-open':
                print('Open folder')


Example().run()

Upvotes: 1

Related Questions