【发布时间】:2021-04-10 12:54:46
【问题描述】:
当我运行这段代码时,它显示“TypeError: 'NoneType' object is not subscriptable”。 当我点击按钮时,我想切换到其他屏幕 我正在编写一个小程序,但遇到了错误“TypeError:'NoneType' 对象不可下标。 我以前从未见过这个错误,所以我不知道它是什么意思。
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 中有语法错误或不正确的属性引用。乍一看,您的 kv 的一个问题是缩进并不总是正确的,并且您在某些地方拼写了
Text而不是text。 -
我认为在我的代码中使用“空格”有问题。但我想不通。
标签: python python-3.x kivy screen kivy-language