【发布时间】:2021-10-30 06:14:40
【问题描述】:
我想将用户的一些数据保存在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()
请帮我找出解决办法。
【问题讨论】:
-
save_bio方法在哪里被调用?显然,当它不应该被传递时,它被传递了一个参数(假设它被作为Mode类的实例上的方法调用)。您发布的代码未显示使用此方法的位置。这就是正在发生的一切...... -
你需要做 MVP Darshana。当你试图解释你想要什么时,当你可以解释正在发生的事情以及它什么时候崩溃时,你迷失了自己。 stackoverflow.com/help/minimal-reproducible-example
-
@Iguananaut ,我为这个错误道歉。现在我已经编辑了。现在,
save_bio方法在函数edit_bio_dialog中被调用。但错误仍在发生。我该怎么办?
标签: python json kivy typeerror kivymd