Search code examples
pythonjsonkivytypeerrorkivymd

TypeError when storing data in a JSON file.(KivyMD Python)


I want to save some data from the user in a JSON file. But when I do that, a TypeError is appeared.

  • TypeError: save_bio() takes 1 positional argument but 2 were given

In this program there are several other times to store data in a JSON file. But This does not happen in that cases.

Here is my Code


from kivy.lang.builder import Builder
from kivy.storage.jsonstore import JsonStore
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton, MDRaisedButton
from kivymd.uix.dialog import MDDialog

screen_helper = """

ScreenManager:
    EditBio:
    Studio:

<EditBio>:
    name: 'edit_bio'
    orientation: "vertical"
    spacing: "10dp"
    size_hint_y: None
    height: "50dp"
    MDTextField:
        id : bio_text
        text: "Feel Everything"


<Studio>:
    name : 'studio'
    MDRaisedButton:
        text : 'Edit Bio'
        id: edit_profile_pic
        pos_hint: {'center_x':0.5,'center_y':0.5}
        user_font_size: "25sp"
        on_release : app.edit_bio_dialog()
    MDLabel:
        text:'Bio'
        font_style: 'Subtitle1'
        halign: 'center'
        pos_hint : {'center_x':0.255,'center_y':0.538}

"""


class EditBio(Screen):
    pass


class Studio(Screen):
    pass


class Mode(MDApp):
    bio_dialog = None

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

    def save_bio(self):
        self.bio = self.root.get_screen('edit_bio').ids.bio_text.text

        self.store_bio.put('BioInfo', bio=self.bio)
        self.bio_changer()

    def bio_changer(self):
        self.root.get_screen('studio').ids.bio.text = f"{self.store_bio.get('BioInfo')['bio']}"

    def on_start(self):
        self.store_bio = JsonStore("bio.json")
        self.root.get_screen('studio').manager.current = 'studio'

    def edit_bio_dialog(self):
        if not self.bio_dialog:
            self.bio_dialog = MDDialog(
                title="Edit Bio:",
                type="custom",
                content_cls=EditBio(),
                buttons=[
                    MDFlatButton(
                        text="CANCEL", font_style='Button', text_color=self.theme_cls.primary_color
                    ),
                    MDRaisedButton(
                        text="OK", md_bg_color=self.theme_cls.primary_color, on_release=self.save_bio
                    ),
                ],
            )
        self.bio_dialog.open()


Mode().run()

Please help me to find the solution.


Solution

  • When your Button is released, it calls self.save_bio() and passes self and the Button instance as arguments. If you don't use the Button instance in the save_bio() method, just add *args to its definition:

    def save_bio(self, *args):
        self.bio = self.root.get_screen('edit_bio').ids.bio_text.text
    
        self.store_bio.put('BioInfo', bio=self.bio)
        self.bio_changer()