You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy+Python运行报错TypeError: object.__init__()仅接受1个参数问题排查

问题根因

你实例化WorkoutBanner时传入了自定义参数user,但没有提前将user声明为Kivy组件的属性,也没有在调用父类构造方法前把自定义参数从kwargs中移除,导致父类GridLayout拿到了自身不识别的user参数,最终触发object.__init__的参数校验报错。
注意:这个错误发生在你调用super(WorkoutBanner, self).__init__(**kwargs)这一行,还没执行到left_image相关的代码,所以和left_image无关。

修复方案

方案1(推荐,符合Kivy开发规范)

在workoutbanner.py中引入Kivy的字符串属性,提前声明user属性:

from kivy.uix.gridlayout import GridLayout 
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.properties import StringProperty # 新增导入
from firebase_functions import get_profile_picture
from datetime import datetime

class WorkoutBanner(GridLayout):
    rows = 1
    user = StringProperty('') # 提前声明user属性,Kivy会自动处理传入的参数
    
    def __init__(self,**kwargs):
        super(WorkoutBanner, self).__init__(**kwargs)
      
        left = FloatLayout()
        left_image = Image(source= "images/" + self.user, # 直接用self.user即可
        size_hint =(1,0.8), pos_hint={"top":1,"left":1})
        
        left.add_widget(left_image)
        self.add_widget(left)

方案2(无需引入Kivy属性,快速修复)

在调用父类构造方法前先把自定义参数从kwargs中弹出:

class WorkoutBanner(GridLayout):
    rows = 1
  
    def __init__(self,**kwargs):
        # 先取出自定义参数,避免传给父类
        self.user = kwargs.pop('user', '')
        super(WorkoutBanner, self).__init__(**kwargs)
      
        left = FloatLayout()
        left_image = Image(source= "images/" + self.user, 
        size_hint =(1,0.8), pos_hint={"top":1,"left":1})
        
        left.add_widget(left_image)
        self.add_widget(left)

内容的提问来源于stack exchange,提问作者Jeff J. Ohana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:54:03