Home > Enterprise >  How to solve TypeError: ScreenManager.get_screen() missing 1 required positional argument: 'nam
How to solve TypeError: ScreenManager.get_screen() missing 1 required positional argument: 'nam

Time:11-06

I'm fairly new to the kivymd library, and I'm currently developing a small mobile app with it. I'm trying to make this app with multiple screens, and it hasn't been a problem for me to program them to navigate between them with the kvfile. In this particular screen, called "Contraindicaciones", I wanted to use python code to make the design, since there were too many elements in that specific screen and it would have been tedious to make all the elements manually. The rest of the screens are entirely coded with the kvfile. The problem is that when I wanted to make a button to go to the next screen or another screen, I keep having the same error:

TypeError: ScreenManager.get_screen() missing 1 required positional argument: 'name'. It would help me a lot to get that button right :>

Python code:


import kivymd
import kivy
# core and key imports
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.screenmanager import MDScreenManager
from kivy.metrics import dp
from kivy.lang.builder import Builder
from kivy.core.window import Window
# UIX imports
from kivymd.uix.button import MDFillRoundFlatButton
from kivymd.uix.label import MDLabel
from kivymd.uix.pickers import MDTimePicker
from kivymd.uix.scrollview import MDScrollView
from kivymd.uix.gridlayout import MDGridLayout
from kivymd.uix.selectioncontrol import MDCheckbox

class WindowManager(MDScreenManager):
    pass

class MainMenu(MDScreen):
    name = "menu"

class Cincinnati1(MDScreen):
    pass

class Cincinnati2(MDScreen):
    pass

class Cincinnati3(MDScreen):
    pass

class VitalesGlucotest(MDScreen): # Tomar signos etc etc
    pass

class Time(MDScreen):
    pass

class HospitalECV(MDScreen):
    pass

class Tomografia(MDScreen):
    pass

class Hemorragia(MDScreen):
    pass

class CanTrombolisis(MDScreen):
    pass



class Contraindicaciones(MDScreen):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Scrollview
        self.scrollview_contraindicaciones = MDScrollView(size_hint=(1, None), size = (Window.width, Window.height))

        # GridLayout
        layout_contraindicaciones = MDGridLayout(orientation = "lr-tb", spacing = "60dp", padding = "20dp", size_hint_x = 1, size_hint_y = None, cols = 2)
        layout_contraindicaciones.bind(minimum_height = layout_contraindicaciones.setter("height"))

        #title
        title_label = MDLabel(text = "Contraindicaciones", halign="left")
        title_label.font_size = "20dp"
        title_label.size_hint_y = None
        title_label.size_hint_x = 1
        title_label.font_name = "fonts/Lato-Bolditalic.ttf"
        title_label.height = dp(50)

        # Invisible Label
        invisible_label = MDLabel(text = "")
        invisible_label.size_hint_y = None
        invisible_label.size_hint_x = 0.25
        invisible_label.height = dp(50)

        # Title and Invisible Label to not mess the rest up
        layout_contraindicaciones.add_widget(title_label)
        layout_contraindicaciones.add_widget(invisible_label)

        contraindicaciones = ["Hemorragia intracraneal (HIC) previa", "Lesión vascular cerebral estructural conocida",
        "Neoplasia intracraneal maligna conocida ya sea primaria o metastásica", "Accidente cerebrovascular isquémico en un plazo de 3 meses",
        "Sospecha de disección aórtica", "Hemorragia activa o diátesis hemorrágica", "Traumatismo craneoencefálico cerrado importante o traumatismo facial en los últimos 3 meses",
        "Cirugía intracraneal o intramedular en un plazo de 3 meses", "Hipertensión no controlada grave", "En caso de estreptoquinasa, el tratamiento previo debe ser dentro de los 6 meses previos"]

        for contraindicacion in contraindicaciones:
            size_checkbox = dp(32)
            size_labels = dp(14)
            checkbox = MDCheckbox(size_hint = (0.5, None), size = (size_checkbox, size_checkbox))
            label = MDLabel(text = contraindicacion, size_hint = (0.5, None), size = (size_labels, size_labels), font_name = "fonts/Lato-Italic.ttf")
            layout_contraindicaciones.add_widget(label)
            layout_contraindicaciones.add_widget(checkbox)

        # Button and invisible label
        invisible_label2 = MDLabel(text = "")
        invisible_label2.size_hint_y = None
        invisible_label2.size_hint_x = 0.5
        invisible_label2.height = dp(50)

        next_button = MDFillRoundFlatButton(text = "Continuar", font_name = "fonts/Lato-Bolditalic.ttf")
        next_button.haling = "right"
        next_button.size_hint_y = None
        next_button.size_hint_x = 0.5
        next_button.on_release = lambda: WindowManager.switch_to("menu")

        layout_contraindicaciones.add_widget(invisible_label2)
        layout_contraindicaciones.add_widget(next_button)

        #Se agrega Grid a Scroll
        self.scrollview_contraindicaciones.add_widget(layout_contraindicaciones)
        self.add_widget(self.scrollview_contraindicaciones)




class TrombolisisY(MDScreen):
    pass

class NIHSS(MDScreen):
    pass

class NIHSSCalc(MDScreen):
    pass

class Tratamamiento(MDScreen):
    pass

class Gracias(MDScreen):
    pass


class PatricApp(MDApp):
    def build(self):
        Window.clearcolor = (255,255,255,255)
        self.root_widget = Builder.load_file("patric.kv")
        return self.root_widget
    def show_time_picker(self):
        time_dialog = MDTimePicker()
        time_dialog.open()



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

KVfile


WindowManager:
    MainMenu:
    Cincinnati1:
    Cincinnati2:
    Cincinnati3:
    VitalesGlucotest:
    Time:
    HospitalECV:
    Tomografia:
    Hemorragia:
    TrombolisisY:
    Contraindicaciones:
<MainMenu>:
    name: 'menu'

    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"

        Image:
            source: "Images/CAHS.png"

        MDLabel:
            text: "Bienvenido a PATRIC"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "50dp"
            text_color: 0, 0, 0, 1
            halign: "center"
        MDLabel:
            text: "Asistencia de diagnóstico de Infarto Cerebral Isquémico basado en el protocolo ACLS de la American Heart Association de 2020"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "20dp"
            text_color: 0, 0, 0, 1
            halign: "center"

        AnchorLayout:
            anchor_y: "bottom"
            pos_hint: {"bottom": 0.2}
            MDFillRoundFlatButton:
                text: "Iniciar Procedimiento"
                font_name: "fonts/Lato-Bolditalic.ttf"
                font_size: "30dp"
                size_hint: (0.5, 0.5)
                on_release:
                    app.root.current = "contraindicaciones" # solo para testing (cnati_1)

<Cincinnati1>:
    name: "cnati_1"

    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "vertical"
        MDLabel:
            text: "Parálisis Facial"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "35dp"
            size_hint: (1,0.05)
        MDLabel:
            text: "¿El paciente mueve los lados de la cara correctamente?"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
            size_hint: (1,0.005)
        Image:
            source: "Images/Señal1.png"
            padding: 0, 10, 0, 0
            size_hint: (1,0.20)
    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "horizontal"

        MDFillRoundFlatButton:
            text: "Normal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "cnati_2"
        MDFillRoundFlatButton:
            text: "Anormal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            md_bg_color: 1,0,0,1
            on_release:
                app.root.current = "cnati_2"


<Cincinnati2>:
    name: "cnati_2"

    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "vertical"
        MDLabel:
            text: "Desviación de los brazos"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "35dp"
            size_hint: (1,0.05)
        MDLabel:
            text: "¿El paciente mueve ambos brazos correctamente?"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
            size_hint: (1,0.005)
        Image:
            source: "Images/Señal2.png"
            padding: 0, 10, 0, 0
            size_hint: (1,0.20)
    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "horizontal"

        MDFillRoundFlatButton:
            text: "Normal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "cnati_3"
        MDFillRoundFlatButton:
            text: "Anormal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            md_bg_color: 1,0,0,1
            on_release:
                app.root.current = "cnati_3"


<Cincinnati3>:
    name: "cnati_3"

    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "vertical"
        MDLabel:
            text: "Afasia/ Dificultad al hablar"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "35dp"
            size_hint: (1,0.05)
        MDLabel:
            text: "¿El paciente repite la frase correctamente?"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
            size_hint: (1,0.005)
        Image:
            source: "Images/Señal3.png"
            padding: 0, 10, 0, 0
            size_hint: (1,0.20)
    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "horizontal"

        MDFillRoundFlatButton:
            text: "Normal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "VG"
        MDFillRoundFlatButton:
            text: "Anormal"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            md_bg_color: 1,0,0,1
            on_release:
                app.root.current = "VG"

<VitalesGlucotest>:
    name: "VG"
    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"
        MDLabel:
            text: "Tomar signos vitales y hemoglucotest"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "40dp"
            halign: "center"
    AnchorLayout:
        anchor_y: "bottom"
        padding: "15dp", "15dp"
        MDFillRoundFlatButton:
            text: "Siguiente"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "time"

<Time>:
    name: "time"
    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"
        spacing: "10dp"
        MDLabel:
            text: "¿Cuál fue la hora en la que el paciente estuvo normal por última vez... o empezó a presentar síntomas?"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
        MDFillRoundFlatButton:
            multiline: True
            text: "Ingresar Tiempo Transcurrido"
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "20dp"
            size_hint: (1, 0.5)
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            md_bg_color: 0,0.8,0,1
            on_release:
                app.show_time_picker()
        MDFillRoundFlatButton:
            text: "Continuar"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (1, 0.5)
            pos_hint: {"center_x": 0.5, "center_y": 0.25}
            on_release:
                app.root.current = "ECV"

<HospitalECV>:
    name: "ECV"
    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"
        MDLabel:
            text: "Derivar a hospital capacitado para ECV.\n\nAnunciar llegada al centro receptor.\n\nTomar muestras de laboratorio y colocar dos vías venosas"
            multiline: True
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
            halign: "center"
    AnchorLayout:
        anchor_y: "bottom"
        padding: "15dp", "15dp"
        MDFillRoundFlatButton:
            text: "Siguiente"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "tomografia"

<Tomografia>:
    name: "tomografia"
    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"
        MDLabel:
            text: "Realizar"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (1, 0.05)
        MDLabel:
            text: "Derivar a hospital capacitado para ECV.\n\nInterpretación de imagen\n\nEvaluación de neurólogo"
            multiline: True
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "25dp"
            halign: "center"
            pos_hint: {"top": 0.85}
            size_hint: (1, 1)
    AnchorLayout:
        anchor_y: "bottom"
        padding: "15dp", "15dp"
        MDFillRoundFlatButton:
            text: "Siguiente"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "hemo"

<Hemorragia>:
    name:"hemo"

    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "vertical"
        MDLabel:
            text: "¿Las imágenes muestran hemorragia?"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "35dp"
            size_hint: (1,1)
    BoxLayout:
        padding: "15dp", "15dp"
        orientation: "horizontal"

        MDFillRoundFlatButton:
            text: "Si"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            md_bg_color: 1,0,0,1
            on_release:
                app.root.current = "menu"
        MDFillRoundFlatButton:
            text: "No"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            md_bg_color: 0,0.8,0,1
            on_release:
                app.root.current = "t_Y"

<TrombolisisY>:
    name: "t_Y"
    BoxLayout:
        orientation: "vertical"
        padding: "15dp", "15dp"
        MDLabel:
            text: "¡Candidato a trombolisis!\n\nComprobar contraindicaciones y calcular NIHSS."
            multiline: True
            font_name: "fonts/Lato-Italic.ttf"
            font_size: "40dp"
            halign: "center"
    AnchorLayout:
        anchor_y: "bottom"
        padding: "15dp", "15dp"
        MDFillRoundFlatButton:
            text: "Siguiente"
            font_name: "fonts/Lato-Bolditalic.ttf"
            font_size: "30dp"
            size_hint: (0.5, 0.15)
            on_release:
                app.root.current = "contraindicaciones"

<Contraindicaciones>:
    name: "contraindicaciones"

CodePudding user response:

I don't get the error you posted, by I get a similar error:

TypeError: ScreenManager.switch_to() missing 1 required positional argument: 'screen' from line:

next_button.on_release = lambda: WindowManager.switch_to("menu")

In either case, the problem is that you are calling an instance method of ScreenManager as though it were a class method.

One way to fix this is to use the setattr() method of python:

next_button.on_release = lambda: setattr(self.manager, 'current', 'menu')

This sets the current attribute of the ScreenManager instance to menu when the Button is released.

  • Related