kivy : TypeError: 'NoneType' object is not subscriptable in python

kivy : TypeError: 'NoneType' object is not subscriptable in python

当我 运行 这段代码时,它显示“TypeError: 'NoneType' object is not subscriptable”。 我想在单击按钮时切换到其他屏幕 我正在制作一个小程序,我 运行 进入错误“TypeError:'NoneType' object is not subscriptable。 我以前从未见过这个错误,所以我不知道它是什么意思。

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()

这是一个错误 :

 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

您的 kv 文件以 5 个空格的缩进开头,因此您必须在整个 kv 文件中继续使用该缩进大小。 kv 的最后一节缩进不正确,属性 名称无效(Text 应为 text)。这是您的 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                              

新固定码

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()