Matplotlib line graph in KivyMD + Python : TypeError: actualizar_timeline_ventas() takes 1 positional argument but 2 were given

34
April 17, 2021, at 12:40 PM

I am trying to build an app with KivyMD and Python, which will show the user a different time-line graph depending on the period he/she selects. (year, month, week, day). Such selection will be done with checkboxes.

Function actualizar_timeline_ventas will be in charge of creating the graph and such function will be called when the different checkboxes are called. However I encounter a problem. I get TypeError: actualizar_timeline_ventas() takes 1 positional argument but 2 were given when I run the code the way it is. When I delete the code inside the brackets I get the error TypeError: mainloop() missing 1 required positional argument: 'self'.

So I am stuck because I get an error if I have an argument inside the brackets or not. My guess is I am using the incorrect argument, but I really don't know which argument is expected.

Code for minimal reproducible example is the following:

Python code:

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.label import Label
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("module://kivy.garden.matplotlib.backend_kivy")

class LabelButton(ButtonBehavior, Label):
    pass

class MainMenuWindow(Screen):
    pass

class DashboardWindow(Screen):
    # Valores Iniciales
    """ PENDIENTE: HACER QUE LOS VALORES INICIALES VARÍEN EN FUNCIÓN DEL VENDEDOR Y SUS NÚMEROS """
    meta_mensual = 300000
    venta_diaria_ = 2500
    venta_semanal_ = 50000
    venta_mensual_ = 230000
    venta_anual_ = 2500000
    menu = None
    number_items_mdlist = 0
    minutes_items_mdlist = 0
    duracion_actividades_list = []
    picture_filepath_list = []
    image_status_list = []
    self_arg = ''
    # Ventas
    def diario_label_press(self, value):
        if not self.ids.periodo_diario_.active:
            self.ids.periodo_diario_.active = True
        else:
            self.ids.periodo_diario_.active = False
    def semanal_label_press(self, value):
        if not self.ids.periodo_semanal_.active:
            self.ids.periodo_semanal_.active = True
        else:
            self.ids.periodo_semanal_.active = False
    def mensual_label_press(self, value):
        if not self.ids.periodo_mensual_.active:
            self.ids.periodo_mensual_.active = True
        else:
            self.ids.periodo_mensual_.active = False
    def anual_label_press(self, value):
        if not self.ids.periodo_anual_.active:
            self.ids.periodo_anual_.active = True
        else:
            self.ids.periodo_anual_.active = False
    def switch_checkbox(self):
        if self.ids.periodo_diario_.active:
            # Meta de Ventas
            meta_diaria = int(self.meta_mensual) / 30
            meta_diaria_format = '{:,}'.format(meta_diaria)
            self.ids.meta_label.text = "$ " + str(meta_diaria_format) + "0"
            # Monto de Venta
            venta = int(self.venta_diaria_)
            venta = '{:,}'.format(venta)
            self.ids.monto_venta.text = "$ " + str(venta) + ".00"
            # Porcentaje de Meta
            porcentaje = float(self.venta_diaria_ / meta_diaria)
            porcentaje_format = "{0:.1%}".format(porcentaje)
            self.ids.porcentaje_meta.text = str(porcentaje_format)
            self.self_arg = self.venta_diaria_
        elif self.ids.periodo_semanal_.active:
            # Meta de Ventas
            meta_semanal = int(self.meta_mensual) / 4
            meta_semanal_format = '{:,}'.format(meta_semanal)
            self.ids.meta_label.text = "$ " + str(meta_semanal_format) + "0"
            # Monto de Venta
            venta = int(self.venta_semanal_)
            venta = '{:,}'.format(venta)
            self.ids.monto_venta.text = "$ " + str(venta) + ".00"
            # Porcentaje de Meta
            porcentaje = float(self.venta_semanal_ / meta_semanal)
            porcentaje_format = "{0:.1%}".format(porcentaje)
            self.ids.porcentaje_meta.text = str(porcentaje_format)
            self.self_arg = self.venta_semanal_
        elif self.ids.periodo_mensual_.active:
            # Meta de Ventas
            meta_mensual = int(self.meta_mensual) * 1
            meta_mensual_format = '{:,}'.format(self.meta_mensual)
            self.ids.meta_label.text = "$ " + str(meta_mensual_format) + ".00"
            # Monto de Venta
            venta = int(self.venta_mensual_)
            venta = '{:,}'.format(venta)
            self.ids.monto_venta.text = "$ " + str(venta) + ".00"
            # Porcentaje de Meta
            porcentaje = float(self.venta_mensual_ / self.meta_mensual)
            porcentaje_format = "{0:.1%}".format(porcentaje)
            self.ids.porcentaje_meta.text = str(porcentaje_format)
            self.self_arg = self.venta_mensual_
        elif self.ids.periodo_anual_.active:
            # Meta de Ventas
            meta_anual = int(self.meta_mensual) * 12
            meta_anual_format = '{:,}'.format(meta_anual)
            self.ids.meta_label.text = "$ " + str(meta_anual_format) + ".00"
            # Monto de Venta
            venta = int(self.venta_anual_)
            venta = '{:,}'.format(venta)
            self.ids.monto_venta.text = "$ " + str(venta) + ".00"
            # Porcentaje de Meta
            porcentaje = float(self.venta_anual_ / meta_anual)
            porcentaje_format = "{0:.1%}".format(porcentaje)
            self.ids.porcentaje_meta.text = str(porcentaje_format)
            self.self_arg = self.venta_mensual_
        elif not self.ids.periodo_diario_.active and not self.ids.periodo_semanal_.active and \
                not self.ids.periodo_mensual_.active and not self.ids.periodo_anual_.active:
            # Meta de Ventas
            self.ids.meta_label.text = "Seleccione una opción"
            self.ids.monto_venta.text = "$ 0.00"
            self.ids.porcentaje_meta.text = "0.00 %"
        self.actualizar_timeline_ventas(self.self_arg)
    def actualizar_timeline_ventas(self):
        print('Actualizar timeline ventas')
        year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
        unemployment_rate = [9.8, 12, 8, 7.2, 6.9, 7, 6.5, 6.2, 5.5, 6.3]
        plt.plot(year, unemployment_rate, color='blue', marker='o')
        plt.title('Unemployment Rate Vs Year', fontsize=14)
        plt.xlabel('Year', fontsize=14)
        plt.ylabel('Unemployment Rate', fontsize=14)
        plt.grid(True)
        plt.show()

class WindowManager(ScreenManager):
    pass

class Linegraph(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal"
        return WindowManager()
if __name__ == "__main__":
    Linegraph().run()

Kivy Code:

<WindowManager>:
    id: screen_manager
    DashboardWindow:
        id: dash
        name: 'dash'
<DashboardWindow>:
    id: dash
    name:'dash'
    MDBoxLayout:
        orientation: 'vertical'
        valign: 'middle'
        padding: '10dp'
        MDLabel:
            text: "Nombre del Vendedor"
            size_hint_y: 0.15
            font_size: (root.width**2 + root.height**2) / 12.75**4
            valign: 'middle'
            bold: True
        MDBoxLayout:
            orientation: 'horizontal'
            spacing: '5dp'
            size_hint_y: 0.15
            MDLabel:
                text: "Meta de venta:"
                valign: 'middle'
                font_size: (root.width**2 + root.height**2) / 14.25**4
            MDLabel:
                id: meta_label
                text: 'Seleccione una opcion'
                valign: 'middle'
                font_size: (root.width**2 + root.height**2) / 14.25**4
                bold: True
        MDBoxLayout:
            orientation: 'horizontal'
            spacing: '5dp'
            size_hint_y: 0.20
            MDCheckbox:
                group: 'periodo_venta'
                id: periodo_diario_
                size_hint: None, None
                size: dp(50), dp(50)
                pos_hint: {"x":0, "center_y":0.5}
                on_active:
                    root.switch_checkbox()
            LabelButton:
                text: 'Diario'
                color: 0, 0, 0, 1
                halign: 'left'
                valign: 'middle'
                text_size: self.size
                on_release: root.diario_label_press(root.ids.periodo_diario_.active)
            MDCheckbox:
                group: 'periodo_venta'
                id: periodo_semanal_
                size_hint: None, None
                size: dp(50), dp(50)
                pos_hint: {"x":0, "center_y":0.5}
                on_active:
                    root.switch_checkbox()
            LabelButton:
                text: 'Semanal'
                color: 0, 0, 0, 1
                halign: 'left'
                valign: 'middle'
                text_size: self.size
                on_release: root.semanal_label_press(root.ids.periodo_semanal_.active)
            MDCheckbox:
                group: 'periodo_venta'
                id: periodo_mensual_
                size_hint: None, None
                size: dp(50), dp(50)
                pos_hint: {"x":0, "center_y":0.5}
                on_active:
                    root.switch_checkbox()
            LabelButton:
                text: 'Mensual'
                color: 0, 0, 0, 1
                halign: 'left'
                valign: 'middle'
                text_size: self.size
                on_release: root.mensual_label_press(root.ids.periodo_mensual_.active)
            MDCheckbox:
                group: 'periodo_venta'
                id: periodo_anual_
                size_hint: None, None
                size: dp(50), dp(50)
                pos_hint: {"x":0, "center_y":0.5}
                on_active:
                    root.switch_checkbox()
            LabelButton:
                text: 'Anual'
                color: 0, 0, 0, 1
                halign: 'left'
                valign: 'middle'
                text_size: self.size
                on_release: root.anual_label_press(root.ids.periodo_anual_.active)
        MDBoxLayout:
            orientation: 'horizontal'
            padding: 0, '10dp', 0, 0
            spacing: '20dp'
            size_hint: 1, 0.25
            MDCard:
                orientation: 'vertical'
                padding: '10dp'
                size_hint: 0.4, 1
                radius: [16, ]
                md_bg_color: [1, 1, 1, 0.85]
                MDLabel:
                    id: monto_venta
                    text: "$ 0.00"
                    theme_text_color: "Custom"
                    text_color: 0, 0, 0, 1
                    font_style: 'H1'
                    halign: 'center'
                    font_size: (root.width**2 + root.height**2) / 13**4
                MDLabel:
                    text: "Monto Vendido"
                    theme_text_color: "Custom"
                    text_color: 0, 0, 0, 1
                    font_style: 'Subtitle2'
                    halign: 'center'
            MDCard:
                size_hint: 0.4, 1
                orientation: 'vertical'
                padding: '10dp'
                radius: [16, ]
                md_bg_color: [1, 1, 1, 0.85]
                MDLabel:
                    id: porcentaje_meta
                    text: '0.00 %'
                    theme_text_color: "Custom"
                    text_color: 0, 0, 0, 1
                    font_style: 'H1'
                    halign: 'center'
                    font_size: (root.width**2 + root.height**2) / 13**4
                MDLabel:
                    text: "% de meta alcanzado"
                    theme_text_color: "Custom"
                    text_color: 0, 0, 0, 1
                    font_style: 'Subtitle2'
                    halign: 'center'
        MDBoxLayout:
            orientation: 'horizontal'
            spacing: '10dp'
            # padding: "10dp", 0,"10dp", "10dp"
            size_hint_y: 0.45
            MDCard:
                size_hint: 1, 0.9
                orientation: 'vertical'
                radius: [16, ]
                MDBoxLayout:
                    size_hint: 1, 0.175
                    radius: [16, 16, 0, 0]
                    md_bg_color: 0.114, 0.212, 0.235, 1
                    MDLabel:
                        text: 'Lineas de tiempo'
                        halign: 'center'
                        theme_text_color: "Custom"
                        text_color: 1, 1, 1, 1
                MDSeparator:
                    height: "2dp"
                    color: 0.95, 0.81, 0.25, 1
                MDBoxLayout:
                    id: timeline_container
                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                    radius: [0, 0, 16, 16]
                    MDLabel:
                        id: label_timeline_vacio
                        text: 'Seleccionar temporalidad para visualizar ventas del periodo'
                        halign: 'center'

For now the graph does not really change and does not use the information I want to use, I really just want the app to run without errors as the window of the graph is shown.

On the other hand I really would like to understand why I am getting such errors and what is expected when I call functions the way I did. I am confused on why there are sometimes in which you can call a function without an argument and others in which it is a must.

Any suggestions? Thanks a lot in advance.

READ ALSO
Controller Advice bean not instantiated at proper order in Spring Boot 2.4

Controller Advice bean not instantiated at proper order in Spring Boot 2.4

I'm implementing an global exception handling using ControllerAdvice and have trouble having it working with Spring Boot 24

57
What should I enter to the connection string to connect my NodeJS application to a MongoDB server?

What should I enter to the connection string to connect my NodeJS application to a MongoDB server?

I am trying to connect to a MongoDB to a nodejsI have the MongoDB running and I have an ENV file with a connection string variable

33
How to achieve accordion functionality in this?

How to achieve accordion functionality in this?

I am trying to achieve bootstrap accordion like functionality in this code, what I want is one collapse should hide when other one opensI am using bootstrap 3

60