I'm trying to create an app for student presence control and I'm not getting access to a function inside a class in python.

I have tried in many ways without success.

arq.py

class Tela(ScreenManager):
    pass
class Chamada(Screen):
    def chamada_func(self):
        self.ids.grid.add_widget(Label(text = 'teste')) 
        #I would like to press the call button, add the grid label

class Nota(Screen):
    pass
class Prg(App):
    def build(self):
        self.Chamada = Chamada()
        return Tela()

Prg().run()

arq.kv

<Tela>:
    Screen:
        name: 'Principal'
        BoxLayout:
            id: box_principal
            orientation: 'vertical'
            padding: 50
            spacing: 100
            Button:
                text: 'Chamada'
                on_release: root.current = 'Chamada'
            Button:
                text: 'Nota'
                on_release: root.current = 'Nota'   
            Button:
                text: 'Sair'
    Chamada:
        name: 'Chamada'
        BoxLayout:
            id: box_chamada
            orientation: 'vertical'
            GridLayout:
                id: grid

1 Answers

0
ikolim On

Question 2

use this on a button inside the class: how would it be?

Solution 2: kv file - Invoke a method from the class that implemented it

The following solution illustrates how to invoke the method, chamada_func() implemented in class Chamada() from another children within the class via kv file.

        on_release: root.chamada_func()

Snippets

<Chamada>:
    name: 'Chamada'

    BoxLayout:
        id: box_chamada
        orientation: 'vertical'

        GridLayout:
            id: grid

        Button:
            text: 'ok'
            size_hint: 1, 0.05
            on_release: root.chamada_func()

Question 1

How to access a function?

Solution 1: kv file - Invoke a method from another class / screen

The following solution illustrates how to invoke the method, chamada_func() implemented in class Chamada() from another class / screen in kv file.

  • Add an id: chamada to screen, Chamada:
  • Invoke method with on_release: root.ids.chamada.chamada_func()
  • Remove self.Chamada = Chamada() from build() method because you don't want two instances of object, Chamada. On top of that, the instance instantiated in the kv file will have a view / presentation, while the other one instantiated in the build method does not have a view.
  • In kv file, add a class rule, <Chamada>:

Snippets

main.py
class Tela(ScreenManager):
    pass

class Chamada(Screen):
    def chamada_func(self):
        self.ids.grid.add_widget(Label(text = 'teste')) 


class Nota(Screen):
    pass


class Prg(App):
    def build(self):
        return Tela()

Prg().run()
kv file
<Tela>:
    Screen:
        name: 'Principal'
        BoxLayout:
            id: box_principal
            orientation: 'vertical'
            padding: 50
            spacing: 100
            Button:
                text: 'Chamada'
                on_release: 
                    root.ids.chamada.chamada_func()
                    root.current = 'Chamada'
            Button:
                text: 'Nota'
                on_release: root.current = 'Nota'   
            Button:
                text: 'Sair'
    Chamada:
        id: chamada

<Chamada>:
    name: 'Chamada'

    BoxLayout:
        id: box_chamada
        orientation: 'vertical'

        GridLayout:
            id: grid

        Button:
            text: 'ok'
            size_hint: 1, 0.05
            on_release: root.chamada_func()