[英]How do I stop the buttons from getting pressed twice in kivy
How do I prevent the buttons from getting pressed twice.如何防止按钮被按下两次。 I only want the buttons to be pressed once and the items to be stored only once.我只希望按钮被按下一次并且项目只被存储一次。 Because if the buttons get pressed more than once it will store the value more than once in the val_holder list因为如果按钮被多次按下,它将在 val_holder 列表中多次存储该值
import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label
val_holder=[]
class LandingScreen(FloatLayout):
def __init__(self, **kwargs):
super(LandingScreen, self).__init__(**kwargs)
# put whatever pos_hint value you want.
self.btn1=Button(text='button1 ', size_hint=(0.5, 0.5),
on_press=self.click_b1))
self.btn2=Button(text='button1 ', size_hint=(0.5, 0.5),
on_press=self.click_b2))
self.btn3=Button(text='button1 ', size_hint=(0.5, 0.5),
on_press=self.click_b3))
self.add_widget(self.btn1)
self.add_widget(self.btn2)
self.add_widget(self.btn3)
def click_b1(self, instance):
val_holder.append('a')
total_item() # new line
def click_b2(self, instance):
val_holder.append('b')
total_item() # new line
def click_b3(self, instance):
val_holder.append('c')
total_item() # new line
def total_item():
print(len(val_holder))
class SplashApp(App):
def build(self):
return LandingScreen()
if __name__ == '__main__':
SplashApp().run()
You could disable the button after it is clicked once:单击一次后,您可以禁用该按钮:
def click_b1(self, instance):
val_holder.append('a')
total_item() # new line
instance.disabled = True
Or you could check if a
is already in val_holder:或者您可以检查a
是否已经在 val_holder 中:
def click_b1(self, instance):
if 'a' not in val_holder:
val_holder.append('a')
total_item() # new line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.