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

153 views
Skip to first unread message

Jeremy Donahue

unread,
Apr 20, 2020, 10:28:40 PM4/20/20
to Kivy users support
New Kivy user.

Currently working through the following documentation.

Attempting to run the code on page 36.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class LoginScreen(GridLayout):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label('User Name:'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)
        self.add_widget(Label('Password:'))
        self.password = TextInput(password=True, multiline=False)
        self.add_widget(self.password)


class MyApp(App):
    def build(self):
        return LoginScreen()


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

I receive the following error.
I do not understand what it won't run.
As far as I can tell my code is exactly like the code in the guide.

(tut) Jeremys-MacBook-Pro:tut home$ python my.py

[INFO   ] [Logger      ] Record log in /Users/home/.kivy/logs/kivy_20-04-20_0.txt

[INFO   ] [Kivy        ] v1.11.1

[INFO   ] [Kivy        ] Installed at "/Users/home/Documents/python-virtual-environments/tut/lib/python3.7/site-packages/kivy/__init__.py"

[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 

[Clang 6.0 (clang-600.0.57)]

[INFO   ] [Python      ] Interpreter at "/Users/home/Documents/python-virtual-environments/tut/bin/python"

[INFO   ] [Factory     ] 184 symbols loaded

[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)

[INFO   ] [Text        ] Provider: sdl2

[INFO   ] [Window      ] Provider: sdl2

[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system

[INFO   ] [GL          ] Backend used <sdl2>

[INFO   ] [GL          ] OpenGL version <b'2.1 INTEL-12.10.14'>

[INFO   ] [GL          ] OpenGL vendor <b'Intel Inc.'>

[INFO   ] [GL          ] OpenGL renderer <b'Intel Iris OpenGL Engine'>

[INFO   ] [GL          ] OpenGL parsed version: 2, 1

[INFO   ] [GL          ] Shading version <b'1.20'>

[INFO   ] [GL          ] Texture max size <16384>

[INFO   ] [GL          ] Texture max units <16>

[INFO   ] [Window      ] auto add sdl2 input provider

[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked

 Traceback (most recent call last):

   File "my.py", line 24, in <module>

     MyApp().run()

   File "/Users/home/Documents/python-virtual-environments/tut/lib/python3.7/site-packages/kivy/app.py", line 829, in run

     root = self.build()

   File "my.py", line 20, in build

     return LoginScreen()

   File "my.py", line 10, in __init__

     self.add_widget(Label('User Name:'))

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

Robert Flatt

unread,
Apr 20, 2020, 11:13:40 PM4/20/20
to Kivy users support
Use:

Label(text="whatever")

Because:
1) this is Object Oriented Programming , there is an implicit     self    as the first positional argument  - that is the 1 positional argument
2) Label only takes one positional argument, the  (optional) other arguments use named association - by just specifying "whatever" you created a second positional argument.

Hence the error expected 1 positional argument but 2 were given.

So use named association for the text value

Jeremy Donahue

unread,
Apr 20, 2020, 11:29:37 PM4/20/20
to Kivy users support
Thank you.
Reply all
Reply to author
Forward
0 new messages