Python KivyMD。如何通过点击同一个按钮轮流运行两个函数?
Python KivyMD. How to run two functions in turn by clicking on the same button?
也就是说,他按下按钮,1号功能启动,再次按下,2号功能启动。再次按下1号功能,依此类推...如何使用单个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()
MDFloatingActionButton:
icon: "flashlight"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release:
app.foo1()
app.foo2()
应该就可以了
编辑:
抱歉,我好像误解了你的问题。这是你应该怎么做
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()
也就是说,他按下按钮,1号功能启动,再次按下,2号功能启动。再次按下1号功能,依此类推...如何使用单个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()
MDFloatingActionButton:
icon: "flashlight"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release:
app.foo1()
app.foo2()
应该就可以了
编辑: 抱歉,我好像误解了你的问题。这是你应该怎么做
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()