1
votes

If anyone can help me I would appreciate it. I am trying to initialize a kivy screen into another kivy screen which is something I struggle with, I've tried different methods to initialize it and I keep receiving error codes. I think I have something to do with the way my GUI is set up but I'm not sure. My latest error code is this:

TypeError: __init__() takes 1 positional argument but 2 were given

I have been trying different methods to initialize the ProjectListScreen in the ApplyPage. can someone please help identify exactly what I'm doing wrong I'll appreciate it. Below is my code:

class ProjectListScreen(Screen):
    project_list = ObjectProperty(None)
    def __init__(self, **kwargs):
        super(ProjectListScreen, self).__init__(**kwargs)
        self.thelocalId = None
        self.placementtext = None

projectlistscreen = ProjectListScreen()

class ApplyPage(Screen):

    def __init__(self, **kwargs):
        super(ApplyPage, self).__init__(**kwargs)
        self.projectlistscreen = projectlistscreen
        self.yes = Button(text="Yes", font_size = 20, font_name= "fonts/Qanelas-Heavy.otf", background_color = (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint = {"x":0.1,"y":0.05}, size_hint= [0.2, 0.1])
        self.add_widget(self.yes)
        self.no = Button(text="No", font_size= 20, font_name= "fonts/Qanelas-Heavy.otf", background_color = (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint = {"x":0.7, "y":0.05}, size_hint= [0.2, 0.1])
        self.add_widget(self.no)

    def on_enter(self, *args):
        print(self.projectlistscreen.placementtext)

class MyApp(App):
    refresh_token_file = "refresh_token.txt"
    members_list = "members_list.txt"




    def build(self):
        self.refresh_token_file = self.user_data_dir + self.refresh_token_file
        self.thefirebase = MyFireBase()
        self.projectlistscreen = ProjectListScreen()
        self.apply = ApplyPage(self.projectlistscreen)



        return sm



sm = Builder.load_file("kivy.kv")

if __name__ == "__main__":
    MyApp().run()

Below is my full traceback

Traceback (most recent call last):
File "/Users/temitayoadefemi/PycharmProjects/test5/mainfile.py", line 871, in
MyApp().run()
File "/Users/temitayoadefemi/PycharmProjects/test5/venv/lib/python3.7/site-packages/kivy/app.py", line 800, in run
root = self.build()
File "/Users/temitayoadefemi/PycharmProjects/test5/mainfile.py", line 860, in build
self.apply = ApplyPage(self.projectlistscreen)
TypeError: init() takes 1 positional argument but 2 were given

1
@UlrichEckhardt : I think this is not a problem about the class instance self, but rather about the difference between *args and **kwargsphoenixo

1 Answers

0
votes

There is a difference between arguments *args and keyword arguments **kwargs. When you call self.apply = ApplyPage(self.projectlistscreen), you give 2 arguments to the the __init__ function : the class instance self and self.projectlistscreen, but the function only expected the class instance self + eventual keyword arguments.

Here an example to understand better :

def myfun1(**kwargs):
    print(kwargs)

myfun1(3)
# TypeError: myfun() takes 0 positional arguments but 1 was given

def myfun2(**kwargs):
    print(kwargs)

myfun2(foobar=3)
# {'foobar': 3}

def myfun3(*args):
    print(args)

myfun3(3)
# (3,)

def myfun4(*args):
    print(args)

myfun4(foobar=3)
#TypeError: myfun() got an unexpected keyword argument 'foobar'

So you have 2 choices for your problem :

def __init__(self, *args):

Or

self.apply = ApplyPage(foobar=self.projectlistscreen)