Jeff J. Ohana
Jeff J. Ohana

Reputation: 23

Kivy and Python TypeError: object.__init__() takes exactly one argument (the instance to initialize)

I have two files among other kivy files as shown below. When running the main file, I am getting the following error:

File "C:\Users\JeffO\OneDrive\Desktop\Gym App\workoutbanner.py", line 13, in __init__
    super(WorkoutBanner, self).__init__(**kwargs)
  File "C:\Users\JeffO\anaconda3\lib\site-packages\kivy\uix\gridlayout.py", line 279, in __init__
    super(GridLayout, self).__init__(**kwargs)
  File "C:\Users\JeffO\anaconda3\lib\site-packages\kivy\uix\layout.py", line 76, in __init__
    super(Layout, self).__init__(**kwargs)
  File "C:\Users\JeffO\anaconda3\lib\site-packages\kivy\uix\widget.py", line 350, in __init__
    super(Widget, self).__init__(**kwargs)
  File "kivy\_event.pyx", line 245, in kivy._event.EventDispatcher.__init__
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Can you help me figure out what is wrong? I am expecting to obtain a profile picture on the main screen. I suspect the left_image in the second file to cause the problem but I am not sure.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
import requests
import json
from workoutbanner import WorkoutBanner


class HomeScreen(Screen):
    pass
class ImageButton(ButtonBehavior,Image):
    pass

class SigninScreen(Screen):
    pass

GUI = Builder.load_file("main.kv")

class MainApp(App):
    my_user_id = 'user1'
    user_table = "users"
    activity_table = "activities"
    def build(self):
        return GUI
    
    def on_start(self):
        #Query database data
        result_users = requests.get("https://uniquedatabase-c4647-default-rtdb.firebaseio.com/" + self.user_table + ".json")
        result_activities = requests.get("https://uniquedatabase-c4647-default-rtdb.firebaseio.com/" + self.activity_table + ".json")
        data_users = json.loads(result_users.content.decode())
        data_activities = json.loads(result_activities.content.decode())
        
        streak_label = self.root.ids['home_screen'].ids['streak_label']
        streak_label.text = str(data_users[self.my_user_id]['streak'])
        
        banner_grid = self.root.ids['home_screen'].ids['banner_grid']
        for workouts in data_activities.values():
            W = WorkoutBanner(user=workouts['user'])
            banner_grid.add_widget(W)
            
        
        #Fill HomeScreen feed
    def change_screen(self,screen_name):
        screen_manager = self.root.ids["screen_manager"]
        screen_manager.current = screen_name
        
MainApp().run()
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 firebase_functions import get_profile_picture
from datetime import datetime

class WorkoutBanner(GridLayout):
    
    rows = 1
  
    def __init__(self,**kwargs):
        super(WorkoutBanner, self).__init__(**kwargs)
      
        left = FloatLayout()
        left_image = Image(source= "images/" + kwargs['user'], 
        size_hint =(1,0.8), pos_hint={"top":1,"left":1})
        
        left.add_widget(left_image)
        
        self.add_widget(left)

Upvotes: 0

Views: 1266

Answers (1)

John Anderson
John Anderson

Reputation: 38822

The line of code:

W = WorkoutBanner(user=workouts['user'])

is providing a kwargs argument to WorkoutBanner, but your __init__() method of WorkoutBanner does not handle that kwarg. As a result, that kwargs is passed to the super:

super(WorkoutBanner, self).__init__(**kwargs)

and that in turn passes the kwargs to its super. And so on until it reaches the EventDispatcher.__init__() where it causes an exception.

I believe you can fix that by just adding a Property to WorkoutBanner for the user attribute:

class WorkoutBanner(GridLayout):
    rows = 1
    user = StringProperty('')

This assumes that the user is a String.

Upvotes: 2

Related Questions