5

When I run this code, it reveals that "TypeError: 'NoneType' object is not subscriptable". I Want to switch to others screen when I Click to buttons I'm making a small program and I ran into the error "TypeError: 'NoneType' object is not subscriptable. I have never before seen this error, so I have no idea what it means.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen


#define our different screens

class StartTestBtn(Screen):
     pass

class TestsResultBtn(Screen):
     pass

class SettingBtn(Screen):
     pass

class ContactUsBtn(Screen):
     pass

class MainPage(Screen):
     pass

class WindowManager(ScreenManager):
     pass


class MyGridLayout(Widget):
     pass


Builder.load_string("""
#:import utils kivy.utils
WindowManager:
     MainPage:
     StartTestBtn:
     TestsResultBtn:
     SettingBtn:
     ContactUsBtn:


<MainPage>:
     name:"main page"

     BoxLayout:
          cols:1
          size: root.width , root.height
          spacing: 20
          padding: 150

          Label:
               text: "Welcom"
               font_size:72
  
          Button:
               text: "start Test"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "startTestBtn"
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)

          Button:
               text: "Tests result"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "TestsResultBtn" 
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1) 

          Button:
               text: "Setting"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "SettingBtn"  
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)

          Button:
               text: "Contact Us"
               font_size: 32            
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "contactUsBtn"  
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)     
<StartTestBtn>:
     name: "startTestBtn"

          Label:
          Text: "start"
          font_size: 72
<TestsResultBtn>:
     name: "TestsResultBtn"

          Label:
          Text: "start"
          font_size: 72
<SettingBtn>:
     name: "SettingBtn"

          Label:
          Text: "start"
          font_size: 72
<ContactUsBtn>:
     name: "contactUsBtn"

          Label:
          Text: "contact"
          font_size: 72                              

""")


class MyLayout(Widget):
     pass

class Shenacell(App):
     def build(self):
         return MyLayout()

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

and this is an error :

 Traceback (most recent call last):
   File "f:/venv/nowornever/Scripts/gui_python.py", line 32, in <module>
     Builder.load_string("""
   File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\kivy\lang\builder.py", line 373, 
in load_string
     parser = Parser(content=string, filename=fn)
   File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\kivy\lang\parser.py", line 402, in __init__
     self.parse(content)
   File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\kivy\lang\parser.py", line 511, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "C:\Users\Asus\AppData\Roaming\Python\Python38\site-packages\kivy\lang\parser.py", line 674, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable
siitaw
  • 107
  • 3
  • 12
  • 1
    I'm not sure quite how you've hit this particular error, but it probably indicates a syntax error or incorrect property reference in your kv. At a glance, one problem with your kv is that the indentaction isn't always correct and you typo `Text` instead of `text` in places. – inclement Apr 10 '21 at 13:08
  • I think I have problem to use "spaces" in my code . but I can't figure out it . – siitaw Apr 10 '21 at 14:02
  • 3
    **Can we agree that the Kivy parser is awful?** I just hit the same error after foolishly copy-pasting a snippet from GitHub with a differing indentation level into my `.kv` file. Cue instant low-level `"TypeError: 'NoneType' object is not subscriptable"` exception from the Kivy parser. I knew the snippet was the culprit – but I didn't know why. I *never* would have guessed that the Kivy parser required **every line of a `.kv` file to be indented by a multiple of the same number of spaces** or that this exception derives from that requirement. In short, this is insane. `` – Cecil Curry Jun 17 '22 at 05:30

2 Answers2

7

Your kv file starts with indentations of 5 spaces, so you must continue that indentation size throughout the kv file. Your last section of kv has incorrect indentation as well as invalid property name (Text should be text). Here is a corrected version of your kv:

#:import utils kivy.utils
WindowManager:
     MainPage:
     StartTestBtn:
     TestsResultBtn:
     SettingBtn:
     ContactUsBtn:


<MainPage>:
     name:"main page"

     BoxLayout:
          cols:1
          size: root.width , root.height
          spacing: 20
          padding: 150

          Label:
               text: "Welcom"
               font_size:72

          Button:
               text: "start Test"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "startTestBtn"
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)

          Button:
               text: "Tests result"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "TestsResultBtn" 
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1) 

          Button:
               text: "Setting"
               font_size: 32
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "SettingBtn"  
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)

          Button:
               text: "Contact Us"
               font_size: 32            
               size_hint_x: None
               height:50
               size_hint_y: None
               width:200
               on_release: app.root.current = "contactUsBtn"  
               size_hint: {'center_x': 0.5}
               size_hint: (.5,.5)
               background_color : (34/255.0,59/255.0,74/255.0,1)     
<StartTestBtn>:
     name: "startTestBtn"

     Label:
          text: "start"
          font_size: 72
<TestsResultBtn>:
     name: "TestsResultBtn"

     Label:
          text: "start"
          font_size: 72
<SettingBtn>:
     name: "SettingBtn"

     Label:
          text: "start"
          font_size: 72
<ContactUsBtn>:
     name: "contactUsBtn"

     Label:
          text: "contact"
          font_size: 72                              
John Anderson
  • 35,991
  • 4
  • 13
  • 36
0

new fixed code

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen


#define our different screen



class StartTestBtn(Screen):
    pass

class TestsResultBtn(Screen):
    pass

class SettingBtn(Screen):
    pass

class ContactUsBtn(Screen):
    pass

class MainPage(Screen):
    pass

class WindowManager(ScreenManager):
    pass


class MyGridLayout(Widget):
    pass

Builder.load_string("""

#:import utils kivy.utils
<WindowManager>:
    MainPage:
    StartTestBtn:
    TestsResultBtn:
    SettingBtn:
    ContactUsBtn:



<MainPage>:
    name:"main page"

    BoxLayout:
        cols:1
        size: root.width , root.height
        spacing: 20
        padding: 150
        Label:
            text: "Welcom"
            font_size:72
        Button:
            text: "start Test"
            font_size: 32
            size_hint_x: None
            height:50
            size_hint_y: None
            width:200
            on_release: app.root.current = "startTestBtn"
            #size_hint: {'center_x': 0.5}
            #size_hint: (.5,.5)
            background_color : (34/255.0,59/255.0,74/255.0,1)
        Button:
            text: "Tests result"
            font_size: 32
            size_hint_x: None
            height:50
            size_hint_y: None
            width:200
            on_release: app.root.current = "TestsResultBtn"
            #size_hint: {'center_x': 0.5}
            #size_hint: (.5,.5)
            background_color : (34/255.0,59/255.0,74/255.0,1)
        Button:
            text: "Setting"
            font_size: 32
            size_hint_x: None
            height:50
            size_hint_y: None
            width:200
            on_release: app.root.current = "SettingBtn"
            #size_hint: {'center_x': 0.5}
            #size_hint: (.5,.5)
            background_color : (34/255.0,59/255.0,74/255.0,1)
        Button:
            text: "Contact Us"
            font_size: 32
            size_hint_x: None
            height:50
            size_hint_y: None
            width:200
            on_release: app.root.current = "contactUsBtn"
            #size_hint: {'center_x': 0.5}
            #size_hint: (.5,.5)
            background_color : (34/255.0,59/255.0,74/255.0,1)

<StartTestBtn>:
    name: "startTestBtn"
    Label:
        text: "start"
        font_size: 72
<TestsResultBtn>:
    name: "TestsResultBtn"
    Label:
        text: "start"
        font_size: 72
<SettingBtn>:
    name: "SettingBtn"
    Label:
        text: "start"
        font_size: 72
<ContactUsBtn>:
    name: "contactUsBtn"
    Label:
        text: "contact"
        font_size: 72   

""")



class MyLayout(Widget):
    pass

class Shenacell(App):
    def build(self):
        return WindowManager()

if __name__ == '__main__' :
    Shenacell().run()
  • This would be a better answer if you explained how the code you provided answers the question. – pppery Mar 18 '22 at 02:19