1

Nuevo en python-Kivy, con un proyecto en mente pero no logro hacer que un boton llame a una función. Les muestro mi código:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

kv='''
GridLayout:
    spacing: 10
    padding: 10
    rows:6
    id: registro

    BoxLayout:
        id: cedula
        Label:
            text:'Cédula'
            font_size: 16
        TextInput:
            id: key
            multiline:False
    BoxLayout:
        id: nombre
        Label:
            text:'Nombre y Apellido'
            font_size: 16
        TextInput:
            multiline:False
    BoxLayout:
        id: Correo
        Label:
            text:'Correo Electrónico'
            font_size: 16
        TextInput:
            id: mail
            multiline:False
    BoxLayout:
        id: carga
        Button:
            text:'Registrar'
            font_size: 14
            on_press: app.reg
        Button:
            text:'Cancelar'
            font_size: 14
'''
class GridLayout(GridLayout):
    def reg():
        print ('El botón si llama la función')

class registroApp(App): 

    def build(self):
        return Builder.load_string(kv) 

registroApp().run()

el error que me arroja es:

File "<string>", line 1, in <module>
 NameError: name 'app' is not defined

Como Pueden observar... el botón 'Cancelar' tampoco esta programado con quit() pues ni idea de como hacerlo... Agradecido de antemano

1 respuesta 1

1

Unas cuantas aclaraciones:

  • No deberias usar para la clase de tu widget el mismo nombre que el del layout del que hereda:

    class GridLayout(GridLayout)
    

    Aunque funcione puede ser muy confuso.

  • Para hacer referencia a la clase padre que defines en python debes usar una construccion del tipo en kv languaje.

  • Por otro lado en debes usara root en vez de app al llamar a la función.

  • Por último para llamar a la función/método debes usar parentesis, sino no la ejecutaras.

Este sería el código funcionando:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

kv='''
<MyWidget>
    spacing: 10
    padding: 10
    rows:6
    id: registro

    BoxLayout:
        id: cedula
        Label:
            text:'Cédula'
            font_size: 16
        TextInput:
            id: key
            multiline:False
    BoxLayout:
        id: nombre
        Label:
            text:'Nombre y Apellido'
            font_size: 16
        TextInput:
            multiline:False
    BoxLayout:
        id: Correo
        Label:
            text:'Correo Electrónico'
            font_size: 16
        TextInput:
            id: mail
            multiline:False
    BoxLayout:
        id: carga
        Button:
            text:'Registrar'
            font_size: 14
            on_press: root.reg()
        Button:
            text:'Cancelar'
            font_size: 14
'''

class MyWidget(GridLayout):
    def __init__(self):
        super(MyWidget, self).__init__()

    def reg(self):
        print ('El botón si llama la función')

class registroApp(App):
    def build(self):
        Builder.load_string(kv)
        return MyWidget()

if __name__ == '__main__':
    registroApp().run()

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.