Kivy中AbstractStore.put()参数错误求助:传3参但仅需2个
问题解决:TypeError: AbstractStore.put()需要2个位置参数,但传入了3个
错误原因
Kivy的JsonStore.put()方法不支持直接将字典作为第二个位置参数传入,必须通过关键字参数传递数据。你当前的self.store.put('moveis', {})和self.store.put('moveis', self.moveis)写法不符合API规范,因此触发参数错误。
另外,self.store.get('moveis') or {}的写法存在隐患:如果get返回空字典,or {}会重复替换为空字典,更稳妥的方式是直接使用get方法的默认参数,避免捕获KeyError。
修复步骤
- 替换
try-except逻辑:用self.store.get('moveis', {})直接获取值,键不存在时自动返回空字典,无需异常捕获。 - 修改
put方法调用:通过value关键字参数传递整个字典,或者用**拆包字典(需确保字典键为合法参数名)。
修改后的完整代码
from kivy.config import Config Config.set('graphics', 'window_state', 'dock') from kivy.app import App from kivy.uix.label import Label from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button from kivy.uix.textinput import TextInput from kivy.storage.jsonstore import JsonStore from kivy.uix.behaviors.focus import FocusBehavior class MyTextInput(TextInput, FocusBehavior): pass class LogisticaApp(App): def build(self): self.store = JsonStore('moveis.json') # 直接使用get的默认参数处理键不存在的情况 self.moveis = self.store.get('moveis', {}) layout = BoxLayout(orientation='vertical') self.modelo_label = Label(text="Modelo") layout.add_widget(self.modelo_label) self.modelo_entry = MyTextInput() layout.add_widget(self.modelo_entry) self.tamanho_label = Label(text="Tamanho") layout.add_widget(self.tamanho_label) self.tamanho_entry = MyTextInput() layout.add_widget(self.tamanho_entry) self.cor_label = Label(text="Cor") layout.add_widget(self.cor_label) self.cor_entry = MyTextInput() layout.add_widget(self.cor_entry) self.localizacao_label = Label(text="Localização") layout.add_widget(self.localizacao_label) self.localizacao_entry = MyTextInput() layout.add_widget(self.localizacao_entry) self.resultado_label = Label(text="") layout.add_widget(self.resultado_label) self.consulta_button = Button(text="Consultar", on_press=self.consulta) layout.add_widget(self.consulta_button) self.cadastro_button = Button(text="Cadastrar", on_press=self.cadastro) layout.add_widget(self.cadastro_button) return layout def on_stop(self): # 通过关键字参数传递字典数据 self.store.put('moveis', value=self.moveis) def consulta(self, instance): modelo = self.modelo_entry.text.lower() tamanho = self.tamanho_entry.text cor = self.cor_entry.text if modelo in self.moveis: if tamanho and cor: mensagem = f"{self.moveis[modelo]['localizacao']}: {tamanho} {cor}" elif tamanho: mensagem = f"{self.moveis[modelo]['localizacao']}: {tamanho}" elif cor: mensagem = f"{self.moveis[modelo]['localizacao']}: {cor}" else: mensagem = self.moveis[modelo]['localizacao'] self.resultado_label.text = f" {mensagem}" else: self.resultado_label.text = f"O móvel {modelo} não está cadastrado" def cadastro(self, instance): modelo = self.modelo_entry.text.lower() # 转换为小写 tamanho = self.tamanho_entry.text cor = self.cor_entry.text localizacao = self.localizacao_entry.text if modelo and len(modelo) >= 2: # 验证模型名非空且至少2个字符 self.moveis[modelo] = {'tamanho': tamanho, 'cor': cor, 'localizacao': localizacao} self.resultado_label.text = "Móvel cadastrado com sucesso" else: self.resultado_label.text = "O modelo deve ter pelo menos 2 caracteres" if __name__ == "__main__": LogisticaApp().run()
额外说明
JsonStore.put()的标准用法:第一个参数为键,后续需通过关键字参数传递数据,比如store.put('user', name='Alice', age=25);若要存储整个字典,可使用store.put('key', value=your_dict)或store.put('key', **your_dict)(后者要求字典键为合法Python参数名)。
内容的提问来源于stack exchange,提问作者Franklin Lima
相关产品推荐
相关产品推荐

