Deshabilite el botón kivy usando el archivo .kv

En este artículo, aprenderemos cómo deshabilitar un botón en kivy usando un archivo .kv, hay algunos lugares donde debemos deshabilitar los botones. Veamos cómo hacer eso.

Tutorial de Kivy: aprenda Kivy con ejemplos.

El botón es una etiqueta con acciones asociadas que se activan cuando se presiona el botón (o se suelta después de un clic/toque). Podemos agregar funciones detrás del botón y diseñar el botón. Pero para deshabilitar el botón, tenemos un nombre de propiedad:

disabled that must be true

esta propiedad ayudará a deshabilitar el botón, es decir, el botón estará allí pero no sirve de nada ya que está deshabilitado, no funcionará ninguna funcionalidad del botón.

En este artículo, hemos utilizado Relative Layout para establecer la posición relativa del botón de trabajo y deshabilitado.

Nota: la propiedad deshabilitada se introdujo en la versión 1.8.0. Si desea usarlo, necesita actualizar su marco.

Basic Approach to disable a button

1) import kivy
2) import kivyApp
3) import Widget
4) import Button
5) Set minimum version(optional)
6) Create widget class:
          1) Arrange a callback
          2) Define Callback function
7) create App class
8) create .kv file (name same as the app class):
        1) create Widget
        2) Create Button
        3) Specify requirements
        4) Disable button true if required
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class 

Código para implementar el botón Enfoque para deshabilitar

## Sample Python application demonstrating the 
## How to disable button in Kivy using .kv file 
  
################################################### 
# import modules 
  
import kivy 
  
# base Class of your App inherits from the App class. 
# app:always refers to the instance of your application 
from kivy.app import App 
  
# This layout allows you to set relative coordinates for children. 
from kivy.uix.relativelayout import RelativeLayout 
  
# To change the kivy default settings 
# we use this module config 
from kivy.config import Config 
      
# 0 being off 1 being on as in true / false 
# you can use 0 or 1 && True or False 
Config.set('graphics', 'resizable', True) 
  
  
# creating the root widget used in .kv file 
class RelativeLayout(RelativeLayout): 
    pass
  
# creating the App class in which name 
#.kv file is to be named Btn.kv 
class BtnApp(App): 
    # defining build() 
    def build(self): 
        # returning the instance of root class 
        return RelativeLayout() 
  
# run the app 
if __name__ == "__main__": 
    BtnApp().run() 

Archivo .kv implementación del enfoque

#.kv file implementation of RelativeLayout 
  
<RelativeLayout>: 
  
    # creating the Disabled button 
    Button: 
          
        text:"B1"
  
        background_color: 0.1, 0.5, 0.6, 1
      
        # positioned at 0 % in x axis and 0 % in y axis 
        # from bottom left, i.e x, y = 0, 0 from bottom left: 
        pos_hint: {"x":0.2, "y":.4}
        size_hint: 0.3, 0.2
  
        # Disabling button
        disabled: True
  
        # working button
    Button: 
        text:"B2"
        background_color: 1, 0, 0, 1
        pos_hint: {"x":.6, "y":.4}
        size_hint: 0.3, 0.2
  
                     

Salida:

Aquí, B1 está deshabilitado y B2 está funcionando.

Publicación traducida automáticamente

Artículo escrito por YashKhandelwal8 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *