本文介绍了Kivy,TypeError:"NoneType"对象没有属性"__getitem__"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试运行示例kivy应用程序时,我遇到了上述错误.

While trying to run a sample kivy application, i m facing the above error.

这是python代码:

here is the python code:

main.py.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition

class MyScreenManager(ScreenManager):
    pass

class MainScreen(Screen):
    pass

class MailScreen(Screen):
    pass

class ProjectScreen(Screen):
    pass

class IntroScreen(Screen):
    pass

class GsaMain(BoxLayout):
    pass

class GsamApp(App):
    def build(self):
        return MyScreenManager()

GsamApp().run()

名为gsam.kv的kivy文件

the kivy file named gsam.kv

MyScreenManager:
    IntroScreen:
    MainScreen:
    PrjcScreen:
    MailScreen:
    ChatScreen:

<IntroScreen>:
    name: 'introscreen'
    BoxLayout:
        Label:
            text: 'Welcome to GSAM'
            font_size: 35
    BoxLayout:
        Label:
            text: "Login"
        TextInput:
            id: login
            text: "Login"
        Button:
            text: "Connect"
            on_release: root.current = 'mainpage'
    BoxLayout:
        Button:
            text: 'Project Page'
            font_size: 25
            on_release: app.root.current = 'main'

<MainScreen>:
    name: 'main'
    BoxLayout:
        Label:
            text: 'Your GSAM Portal'
            font_size: 35
    BoxLayout:
        Button:
            text: 'Check Mails'
            font_size: 25
            on_release: app.root.current = 'mailscreen'
            canvas:
            color: rgba: 1,0,0,1
                Rectangle:
                    size: 50, 50
                    Label: "Post Projects"
                    id: projct
                Rectangle:
                    size: 50, 50
                    Label: "Messages"
                    id: msgs

<MailScreen>:
    name: 'mailscreen'
    BoxLayout:
        Label:
            text: 'GSAM Mail'
            font_size: 35
    BoxLayout:
        Button:
            text: 'Chat'
            font_size: 25
            on_release: app.root.current = 'chat'
<ChatScreen>:
    name: 'chatscreen'
    BoxLayout:
        Label:
            text: 'Chat with your Colleagues'
            font_size: 35
    BoxLayout:
        Button:
            text: 'Home'
            font_size: 25
            on_release: app.root.current = 'main'

<PrjcScreen>:
    name: 'prjcscreen'
    BoxLayout:
        Label:
            text: 'Community Projects'
            font_size: 35
    BoxLayout:
        Button:
            text: 'community Projects'
            font_size: 25
            on_release: app.root.current = 'main'
            GridLayout:
                cols: 2
                rows: 4
                padding: 5
                spacing: 5
                    Label:
                        text: "Project Title"
                    TextInput:
                        id: ptitle
                    Label:
                        text: "Project Description"
                    TextInput:
                        id: pdescr

我不知道回溯指向什么.

I can't figure out what is the traceback pointing to.

 Traceback (most recent call last):
   File "main.py", line 28, in <module>
     GsamApp().run()
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/app.py", line 797, in run
     self.load_kv(filename=self.kv_file)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/app.py", line 594, in load_kv
     root = Builder.load_file(rfilename)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1749, in load_file
     return self.load_string(data, **kwargs)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1796, in load_string
     parser = Parser(content=string, filename=fn)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1185, in __init__
     self.parse(content)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1287, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1384, in parse_level
     level + 1, lines[i:], spaces)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1384, in parse_level
     level + 1, lines[i:], spaces)
   File "/home/afidegnum/gsam/local/lib/python2.7/site-packages/kivy/lang.py", line 1437, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object has no attribute '__getitem__'

我需要在python代码中定义on_release函数吗?

do I need to define a function on_release in python code?

推荐答案

main.py

  • 没有ChatScreen.定义一个:

    main.py

    • There's no ChatScreen. Define one:

      class ChatScreen(Screen):
          pass
      

    • ProjectScreen应该重命名为PrjcScreen以匹配gsam.kv中的名称. (或在gsam.kv中更改PrjcScreen)

    • ProjectScreen should be renamed to PrjcScreen to match the name in gsam.kv. (Or Change PrjcScreen in gsam.kv)

      color: rgba: 1,0,0,1
      

      ->

      Color:
          rgba: 1,0,0,1
      

    • Rectangle 不具有Label参数:

    • Rectangle does not have Label parameters:

              Rectangle:
                  size: 50, 50
                  # Label: "Post Projects"  <---
                  id: projct
              Rectangle:
                  size: 50, 50
                  # Label: "Messages"  <---
                  id: msgs
      

    • 这篇关于Kivy,TypeError:"NoneType"对象没有属性"__getitem__"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:36