在 JSON 文件中存储数据时出现类型错误。(KivyMD Python)
TypeError when storing data in a JSON file.(KivyMD Python)
我想将用户的一些数据保存在 JSON
文件中。但是当我这样做时,会出现 TypeError
。
TypeError: save_bio() takes 1 positional argument but 2 were given
在这个程序中还有几次将数据存储在JSON
文件中。但在那种情况下不会发生这种情况。
这是我的代码
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()
请帮我找到解决办法。
当您的 Button
被释放时,它调用 self.save_bio()
并传递 self
和 Button
实例作为参数。如果在save_bio()
方法中不使用Button
实例,只需在其定义中添加*args
:
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()
我想将用户的一些数据保存在 JSON
文件中。但是当我这样做时,会出现 TypeError
。
TypeError: save_bio() takes 1 positional argument but 2 were given
在这个程序中还有几次将数据存储在JSON
文件中。但在那种情况下不会发生这种情况。
这是我的代码
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()
请帮我找到解决办法。
当您的 Button
被释放时,它调用 self.save_bio()
并传递 self
和 Button
实例作为参数。如果在save_bio()
方法中不使用Button
实例,只需在其定义中添加*args
:
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()