Andrukva
Andrukva

Reputation: 3

Python KivyMD. How to run two functions in turn by clicking on the same button?

That is, he pressed the button, function No. 1 started, pressed again, function No. started.2 pressed again and again function No. 1 and so on... How to do it with a single MDFloatingActionButton?

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.button.button import MDFloatingActionButton

KV = '''
MDFloatLayout:

    MDFloatingActionButton:
        icon: "flashlight"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.foo1() and app.foo2()
        
        '''

class Test(MDApp):
    def build(self):
        screen = Builder.load_string(KV)
        return screen


    def foo1(self):
        print("foo1")

    def foo2(self):
        print("foo2")

    
Test().run()

Upvotes: 0

Views: 472

Answers (1)

darkmatter08
darkmatter08

Reputation: 136

 MDFloatingActionButton:
    icon: "flashlight"
    pos_hint: {'center_x': .5, 'center_y': .5}
    on_release: 
        app.foo1()
        app.foo2()

That should do it

Edit: sorry, it seems like i misunderstood your question. here's how you should do it

from kivymd.app import MDApp
from kivy.lang import Builder

KV = '''
MDFloatLayout:
    MDFloatingActionButton:
        icon: "flashlight"
        pos_hint: {'center_x': .5, 'center_y': .5}
        on_release: app.button_on_release()


'''

class Test(MDApp):
    release_count = 0
    def build(self):
        screen = Builder.load_string(KV)
        return screen

    def button_on_release(self):
        if self.release_count == 0:
            self.foo1()
            self.release_count += 1
            return

        if self.release_count == 1:
            self.foo2()

            self.release_count = 0
            return



    def foo1(self):
        print("foo1")

    def foo2(self):
        print("foo2")


Test().run()

Upvotes: 1

Related Questions