简体   繁体   English

如何阻止按钮在 kivy 中被按下两次

[英]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.

相关问题 使用 Kivy 屏幕,如何根据屏幕 1 上按下按钮的文本更改屏幕 2 的标签? - Using Kivy screens, how do I change the label from screen 2 based on the pressed button's text on screen 1? 如何阻止 Simpleaudio 同时播放文件两次? - How do I stop Simpleaudio from playing a file twice simulaneously? 如何阻止 show() 破坏我的单选按钮? - How do I stop show() from disrupting my radio buttons? 如何使用奇异时钟更改按钮的颜色? - How do I use kivy clock to change the color of buttons? Kivy:如何停止分散的窗口小部件与选项卡标题重叠 - Kivy: How do I stop a scatter widget overlapping tab headers 当使用 Autokey 按下某个键时,如何停止 while 循环? - How do I stop a while loop when a key is pressed with Autokey? 如何将纯python中动态创建的按钮添加到以Kivy语言编写的kivy布局中? - How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language? 我正在做一个 kivy 项目......我正在使用从函数创建的切换按钮......我怎么知道选择了什么? - im working on a kivy project... i am using toggle buttons that are create from a function... how do i know what is selected? 如何停止我的方法在 python 中运行两次? - How do I stop my method running twice in python? 如何使用Kivy创建多个按钮? - How can I create multiple buttons with Kivy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM