Search code examples
pythonkivykivy-language

Trying to open a screen from a button inside a popup, kivy


I need to open a screen when a the 'Tensão de Linha [V]' is released, this button is inside a BoxLayout Popup.

I've tried many ways to do it, and the one that at least gave a me an error that I could comprehend is this:

Python code:

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class WindowManager(ScreenManager):
    pass

class HomeScreen(Screen):
    def __init__(self, **kw):
        super(HomeScreen, self).__init__(**kw)

    def mostrar_lista_fabricantes(self):
        show = Fabricantes()
        popupwindow = Popup(title='Fabricantes', 
                            content=show,
                            size_hint=(0.8, 1)
                            )
        popupwindow.open()
    
    def mostrar_lista_calculadoras(self):
        show = Calculadoras()
        popupwindow = Popup(title='Calculadoras',
                            content=show,
                            size_hint=(0.8, 1)
                            )
        popupwindow.open()


class Fabricantes(Screen):
    pass

class Calculadoras(Screen):
    pass


class TensaoDeLinha(Screen):
    def __init__(self, **kw):
        super(TensaoDeLinha, self).__init__(**kw)
    pass

class Corrente(Screen):
    def __init__(self, **kw):
        super(Corrente, self).__init__(**kw)
    pass

class MainApp(App):
    def build(self):
        return WindowManager()

if __name__ == "__main__":
    MainApp().run()

kv file:

#:kivy 1.0

<WindowManager>:
    HomeScreen:
    Fabricantes:

    Calculadoras:
        TensaoDeLinha:
        Corrente:




<HomeScreen>
    name: 'home'
    BoxLayout:
        orientation: 'vertical'
        canvas:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle: 
                pos: self.pos
                size: self.size
        Label:
            text: ""
            size_hint: 1, 0.1
        Button:
            text: "Regulador de Tensão"
            size_hint: 0.9, 1
            pos_hint: {"center_x": .5}  
            on_release: root.mostrar_lista_fabricantes()
        Label:
            id: invisable
            text: ""
        Button:
            text: "Calculadora"
            size_hint: 0.9, 1
            pos_hint: {"center_x": .5}
            on_release: root.mostrar_lista_calculadoras()
        Label:
            id: invisable
            text: ""
        Image:
            source: r'img\Celesc.png'
            size: self.texture_size

<TensaoDeLinha>:
    name: 'TensaoDeLinha'
    BoxLayout:
        orientation: 'vertical'
        canvas:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle: 
                pos: self.pos
                size: self.size
        Button:
            text: 'Voltar'
            on_release:
                app.root.current = 'home'

<Fabricantes>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: "ITB"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Siemens"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Toshiba"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "McGraw"
            pos_hint: {"center_x": 0.5}

<Calculadoras>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: "Tensão de Linha [V]"
            pos_hint: {"center_x": 0.5}
            on_press: 
                app.root.current = 'TensaoDeLinha'
        Button:
            text: "Corrente[A]"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Potência Complexa [VA]"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Potência ativa [W]"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Potência Reativa [VAr]"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "Fator de Potencia [cos phi]"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "PU"
            pos_hint: {"center_x": 0.5}
        Button:
            text: "RTP"
            pos_hint: {"center_x": 0.5}

The error:

Traceback (most recent call last):
   File "c:\Users\09909909901\Área de Trabalho\Projetos Python\SCalc\src\main.py", line 59, in <module>
     CalcParamApp().run()
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\app.py", line 955, in run
     runTouchApp()
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\base.py", line 574, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\base.py", line 339, in mainloop
     self.idle()
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\base.py", line 383, in idle
     self.dispatch_input()
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\base.py", line 334, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\base.py", line 263, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\core\window\__init__.py", line 1660, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\core\window\__init__.py", line 1677, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\popup.py", line 236, in on_touch_down
     return super(Popup, self).on_touch_down(touch)
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\modalview.py", line 266, in on_touch_down
     super().on_touch_down(touch)
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\relativelayout.py", line 306, in on_touch_down
     ret = super(RelativeLayout, self).on_touch_down(touch)
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\lang\builder.py", line 55, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "c:\Users\09909909901\Área de Trabalho\Projetos Python\SCalc\src\main.kv", line 85, in <module>
     app.root.current = 'TensaoDeLinha'
   File "kivy\properties.pyx", line 520, in kivy.properties.Property.__set__
   File "kivy\properties.pyx", line 567, in kivy.properties.Property.set
   File "kivy\properties.pyx", line 606, in kivy.properties.Property._dispatch
   File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1213, in kivy._event.EventObservers._dispatch
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\screenmanager.py", line 1052, in on_current
     screen = self.get_screen(value)
   File "C:\Users\09909909901\AppData\Roaming\Python\Python310\site-packages\kivy\uix\screenmanager.py", line 1078, in get_screen
     raise ScreenManagerException('No Screen with name "%s".' % name)
 kivy.uix.screenmanager.ScreenManagerException: No Screen with name "TensaoDeLinha".

I was expecting it to open the TensaoDeLinha screen when I pressed the 'Tensão de Linha [V]' button, but it does not recgonize the 'TensaoDeLinha' name.


Solution

  • Try unindenting screen declarations Instead of this

    <WindowManager>:
    HomeScreen:
    Fabricantes:
    
    Calculadoras:
        TensaoDeLinha:
        Corrente:
    

    try this:

    WindowManager:
        HomeScreen:
        Fabricantes:
    
        Calculadoras:
        TensaoDeLinha:
        Corrente:
    

    Because if you indent the screens after Calculadoras you have to declare them after calculadoras