Python.kivy – TypeError: 'NoneType' object is not subscriptable
Question:
I’ve faced unknown problem during coding on Python using GUI library Kivy. The problem is that when I execute this code
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import ButtonBehavior
from pathlib import Path
class SignUpScreen(Screen):
def verify_user(self, email, username):
pass
class SignInUsernameScreen(Screen):
def signin_username(self, uname):
pass
class SignInMasterKeyScreen(Screen):
def check_masterkey(self, mk):
pass
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
Builder.load_file('design.kv')
return RootWidget()
if __name__ == '__main__':
MainApp().run()
here is design.kv file:
<SignUpScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Sign Up'
font_size: '20sp'
TextInput:
id: username
hint_text: 'Username'
TextInput:
id: email
hint_text: 'Email'
RelativeLayout:
Button:
text: 'Create an account'
size_hint: 0.3, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.verify_user(root.ids.username.text, root.ids.email.text)
<SignInUsernameScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Sign In'
font_size: '20sp'
TextInput:
id: username
hint_text: 'Username'
RelativeLayout:
Button:
text: 'Sign In'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.signin_username(root.ids.username.text)
Label:
id: username_wrong
text: ''
<SignInMasterkeyScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Login'
font_size: '20sp'
TextInput:
id: masterkey
password: True
hint_text: 'MasterKey'
RelativeLayout:
Button:
text: 'Login master key'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.signin_masterkey(root.ids.masterkey.text)
Label:
id: masterkey_wrong
text: ''
GridLayout:
cols: 2
size_hint: 0.2, 0.2 # 20% of window space
padding: 10, 10
spacing: 10, 0
Button:
text: 'Forgot Password'
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
Button:
text: 'Back'
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
<RootWidget>:
SignUpScreen:
name: 'signup_screen'
SignInScreen:
name: 'signin_screen'
SignInMasterKeyScreen:
name: 'signin_masterkey'
I got this type of error
[INFO ] [Logger ] Record log in /Users/niknmirosh/.kivy/logs/kivy_22-09-28_38.txt
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.9.12 (main, Apr 5 2022, 01:53:17)
[Clang 12.0.0 ]
[INFO ] [Python ] Interpreter at "/Users/niknmirosh/opt/miniconda3/bin/python3"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_imageio, img_dds, img_sdl2, 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-18.8.4'>
[INFO ] [GL ] OpenGL vendor <b'Intel Inc.'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) Iris(TM) Plus Graphics 655'>
[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 "/Users/niknmirosh/Desktop/PasswordManager (Demo)/main.py", line 46, in <module>
MainApp().run()
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/app.py", line 954, in run
self._run_prepare()
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/app.py", line 924, in _run_prepare
root = self.build()
File "/Users/niknmirosh/Desktop/PasswordManager (Demo)/main.py", line 41, in build
Builder.load_file('/Users/niknmirosh/Desktop/PasswordManager (Demo)/design.kv')
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/builder.py", line 305, in load_file
return self.load_string(data, **kwargs)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/builder.py", line 372, in load_string
parser = Parser(content=string, filename=fn)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 483, in __init__
self.parse(content)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 593, in parse
objects, remaining_lines = self.parse_level(0, lines)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 756, in parse_level
if current_property[:3] == 'on_':
TypeError: 'NoneType' object is not subscriptable
What could I have missed that was important? I’ve done almost everything according to the documentation, but the error still comes out.
Answers:
Your indentation in your kv
file is not consistent.
<SignUpScreen>: # no indentation - OK
GridLayout: # one space indentation - OK (This sets the indentation level for the entire file)
cols: 1 # two space indentation - OK (previous indentation plus another level of indentation)
GridLayout: # two space indentation - OK
cols: 1 # four space indentation - ERROR (should be three space indentation)
The id: username_wrong
in the <SignInUsernameScreen>
rule has two spaces indented from the Label:
.
Kivy is very fussy about indentation levels. Using indentation of a single space is legal, but can make it difficult to spot errors like this. I strongly suggest using something like 4 spaces indentation. Also, from the documentation:
The indentation is important and must be consistent. The spacing must
be a multiple of the number of spaces used on the first indented line.
Spaces are encouraged: mixing tabs and spaces is not recommended.
I’ve faced unknown problem during coding on Python using GUI library Kivy. The problem is that when I execute this code
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import ButtonBehavior
from pathlib import Path
class SignUpScreen(Screen):
def verify_user(self, email, username):
pass
class SignInUsernameScreen(Screen):
def signin_username(self, uname):
pass
class SignInMasterKeyScreen(Screen):
def check_masterkey(self, mk):
pass
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
Builder.load_file('design.kv')
return RootWidget()
if __name__ == '__main__':
MainApp().run()
here is design.kv file:
<SignUpScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Sign Up'
font_size: '20sp'
TextInput:
id: username
hint_text: 'Username'
TextInput:
id: email
hint_text: 'Email'
RelativeLayout:
Button:
text: 'Create an account'
size_hint: 0.3, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.verify_user(root.ids.username.text, root.ids.email.text)
<SignInUsernameScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Sign In'
font_size: '20sp'
TextInput:
id: username
hint_text: 'Username'
RelativeLayout:
Button:
text: 'Sign In'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.signin_username(root.ids.username.text)
Label:
id: username_wrong
text: ''
<SignInMasterkeyScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
padding: 15, 15
spacing: 20, 20
Label:
text: 'User Login'
font_size: '20sp'
TextInput:
id: masterkey
password: True
hint_text: 'MasterKey'
RelativeLayout:
Button:
text: 'Login master key'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
on_press: root.signin_masterkey(root.ids.masterkey.text)
Label:
id: masterkey_wrong
text: ''
GridLayout:
cols: 2
size_hint: 0.2, 0.2 # 20% of window space
padding: 10, 10
spacing: 10, 0
Button:
text: 'Forgot Password'
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
Button:
text: 'Back'
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
<RootWidget>:
SignUpScreen:
name: 'signup_screen'
SignInScreen:
name: 'signin_screen'
SignInMasterKeyScreen:
name: 'signin_masterkey'
I got this type of error
[INFO ] [Logger ] Record log in /Users/niknmirosh/.kivy/logs/kivy_22-09-28_38.txt
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/__init__.py"
[INFO ] [Python ] v3.9.12 (main, Apr 5 2022, 01:53:17)
[Clang 12.0.0 ]
[INFO ] [Python ] Interpreter at "/Users/niknmirosh/opt/miniconda3/bin/python3"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_imageio, img_dds, img_sdl2, 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-18.8.4'>
[INFO ] [GL ] OpenGL vendor <b'Intel Inc.'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) Iris(TM) Plus Graphics 655'>
[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 "/Users/niknmirosh/Desktop/PasswordManager (Demo)/main.py", line 46, in <module>
MainApp().run()
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/app.py", line 954, in run
self._run_prepare()
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/app.py", line 924, in _run_prepare
root = self.build()
File "/Users/niknmirosh/Desktop/PasswordManager (Demo)/main.py", line 41, in build
Builder.load_file('/Users/niknmirosh/Desktop/PasswordManager (Demo)/design.kv')
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/builder.py", line 305, in load_file
return self.load_string(data, **kwargs)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/builder.py", line 372, in load_string
parser = Parser(content=string, filename=fn)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 483, in __init__
self.parse(content)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 593, in parse
objects, remaining_lines = self.parse_level(0, lines)
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 696, in parse_level
_objects, _lines = self.parse_level(
File "/Users/niknmirosh/opt/miniconda3/lib/python3.9/site-packages/kivy/lang/parser.py", line 756, in parse_level
if current_property[:3] == 'on_':
TypeError: 'NoneType' object is not subscriptable
What could I have missed that was important? I’ve done almost everything according to the documentation, but the error still comes out.
Your indentation in your kv
file is not consistent.
<SignUpScreen>: # no indentation - OK
GridLayout: # one space indentation - OK (This sets the indentation level for the entire file)
cols: 1 # two space indentation - OK (previous indentation plus another level of indentation)
GridLayout: # two space indentation - OK
cols: 1 # four space indentation - ERROR (should be three space indentation)
The id: username_wrong
in the <SignInUsernameScreen>
rule has two spaces indented from the Label:
.
Kivy is very fussy about indentation levels. Using indentation of a single space is legal, but can make it difficult to spot errors like this. I strongly suggest using something like 4 spaces indentation. Also, from the documentation:
The indentation is important and must be consistent. The spacing must
be a multiple of the number of spaces used on the first indented line.
Spaces are encouraged: mixing tabs and spaces is not recommended.