How do I stop the buttons from getting pressed twice in kivy

Viewed 208

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

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()
1 Answers

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:

def click_b1(self, instance):
             if 'a' not in val_holder:
                 val_holder.append('a') 
             total_item() # new line
Related