Kivy error: Attribute error:'None type' object has no attribute 'text'

Kivy error: Attribute error:'None type' object has no attribute 'text'

将 运行 保持在错误中,显示“属性错误:'None Type' 对象没有属性 'text'。我不知道问题出在哪里。做了很多研究, 但没有看到任何帖子对这个问题进行任何澄清。想法?

这是我的 main.py 文件

from kivy.app import App
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.factory import Factory

from kivy.uix.popup import Popup
from kivy.uix.button import Button
import os
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.graphics import *
from kivy.properties import ObjectProperty

class OptionMenu(GridLayout):

    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    text_input = ObjectProperty(None)
    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        with open(os.path.join(path, filename[0])) as stream:
            self.text_input.text = stream.read()
        self.dismiss_popup()   

    def show_save(self):
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def save(self, path, filename):
        with open(os.path.join(path, filename), 'w') as stream:
            stream.write(self.text_input.text)

        self.dismiss_popup()
class GraphMenu(GridLayout):
    pass
class UnnamedMenu(GridLayout):
    pass

class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)

class SaveDialog(FloatLayout):
    save = ObjectProperty(None)
    text_input = ObjectProperty(None)
    cancel = ObjectProperty(None)



class MainController(FloatLayout):
    pass


class MainApp(App):
    def build(self):
        rt = MainController()
        return rt
Factory.register('Main', cls=MainController)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)

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

这是我的 main.kv 文件:

#kivy 1.0
<OptionMenu>
    cols: 2
 row:10
    Button:
        size_hint_y: 0.05
        text: "Input File"
        on_release: self.parent.show_load()
 Button:
  size_hint_y:0.05
  text:"Save File"
  on_release:self.parent.show_save()
    TextInput:
        id: text_input
  text:''
 RstDocument:
  text: text_input.text
  show_errors: True

<MainController>
    BoxLayout:
        orientation: 'horizontal'
        Accordion:
            size_hint_x: 0.3
            orientation: 'vertical'
            AccordionItem:
                title:'Options'
                OptionMenu:
                    id: 'AppOpt'
   AccordionItem:
    title:'Config'
    UnnamedMenu:
     id:ConfigTab
   AccordionItem:
    title:'Graph'
    GraphMenu:
     id:GraphTab

        FloatLayout:
            size_hint_x: 0.7
            Label:
                
<scroller>

        
<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 10
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)
<SaveDialog>:
    text_input: text_input
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            on_selection: text_input.text = self.selection and self.selection[0] or ''

        TextInput:
            id: text_input
            size_hint_y: None
            height: 30
            multiline: False

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Save"
                on_release: root.save(filechooser.path, text_input.text)

这是完整的错误消息和堆栈转储:

botstrapping Kivy @ C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\
Setting Environment Variables:
#################################
GST_REGISTRY
C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\gstreamer\registry.bin
---------------
GST_PLUGIN_PATH:
C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\gstreamer\lib\gstreamer-1.0
---------------
PATH:
C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\;C:\Users\My.Files\Download
s\Kivy-1.8.0-py2.7-win32\Python27;C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-
win32\tools;C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\Python27\Scripts
;C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\gstreamer\bin;C:\Users\Adit
.Shah\Downloads\Kivy-1.8.0-py2.7-win32\MinGW\bin;C:\Program Files (x86)\RSA Secu
rID Token Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Inte
l\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Window
s\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program F
iles\Common Files\Intel\WirelessCommon\;C:\Program Files\SAS\SharedFiles(32)\For
mats;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio
\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files (
x86)\Microsoft SQL Server0\DTS\Binn\
----------------------------------
PYTHONPATH:
C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy;
----------------------------------
##################################
done bootstraping kivy...have fun!\n
running "python.exe "D:\workspace\kivyWork\GUI\main.py"" \n
[INFO              ] Kivy v1.8.0
[INFO              ] [Logger      ] Record log in C:\Users\My.Files\.kivy\logs\
kivy_15-02-19_77.txt
[INFO              ] [Factory     ] 157 symbols loaded
[DEBUG             ] [Cache       ] register <kv.lang> with limit=None, timeout=
Nones
[DEBUG             ] [Cache       ] register <kv.image> with limit=None, timeout
=60s
[DEBUG             ] [Cache       ] register <kv.atlas> with limit=None, timeout
=Nones
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_pygame, img
_gif (img_pil ignored)
[DEBUG             ] [Cache       ] register <kv.texture> with limit=1000, timeo
ut=60s
[DEBUG             ] [Cache       ] register <kv.shader> with limit=1000, timeou
t=3600s
[INFO              ] [Text        ] Provider: pygame
[DEBUG             ] [Window      ] Ignored <egl_rpi> (import error)
[INFO              ] [Window      ] Provider: pygame(['window_egl_rpi'] ignored)

[DEBUG             ] [Window      ] Display driver windib
[DEBUG             ] [Window      ] Actual window size: 800x600
[DEBUG             ] [Window      ] Actual color bits r8 g8 b8 a8
[DEBUG             ] [Window      ] Actual depth bits: 24
[DEBUG             ] [Window      ] Actual stencil bits: 8
[DEBUG             ] [Window      ] Actual multisampling samples: 4
GLEW initialization succeeded
[INFO              ] [GL          ] OpenGL version <4.2.0 - Build 10.18.10.3621>

[INFO              ] [GL          ] OpenGL vendor <Intel>
[INFO              ] [GL          ] OpenGL renderer <Intel(R) HD Graphics 4400>
[INFO              ] [GL          ] OpenGL parsed version: 4, 2
[INFO              ] [GL          ] Shading version <4.20 - Build 10.18.10.3621>

[INFO              ] [GL          ] Texture max size <16384>
[INFO              ] [GL          ] Texture max units <32>
[DEBUG             ] [Shader      ] Fragment compiled successfully
[DEBUG             ] [Shader      ] Vertex compiled successfully
[DEBUG             ] [ImagePygame ] Load <C:\Users\My.Files\Downloads\Kivy-1.8.
0-py2.7-win32\kivy\kivy\data\glsl\default.png>
[INFO              ] [Window      ] virtual keyboard not allowed, single mode, n
ot docked
[DEBUG             ] [App         ] Loading kv <D:\workspace\kivyWork\GUI\main.k
v>
[DEBUG             ] [Cache       ] register <kv.loader> with limit=500, timeout
=60s
[INFO              ] [Loader      ] using a thread pool of 2 workers
[DEBUG             ] [Cache       ] register <textinput.label> with limit=None,
timeout=60.0s
[DEBUG             ] [Cache       ] register <textinput.width> with limit=None,
timeout=60.0s
[DEBUG             ] [Atlas       ] Load <C:\Users\My.Files\Downloads\Kivy-1.8.
0-py2.7-win32\kivy\kivy\data\..\data\images\defaulttheme.atlas>
[DEBUG             ] [Atlas       ] Need to load 1 images
[DEBUG             ] [Atlas       ] Load <C:\Users\My.Files\Downloads\Kivy-1.8.
0-py2.7-win32\kivy\kivy\data\..\data\images\defaulttheme-0.png>
[DEBUG             ] [ImagePygame ] Load <C:\Users\My.Files\Downloads\Kivy-1.8.
0-py2.7-win32\kivy\kivy\data\..\data\images\defaulttheme-0.png>
[INFO              ] [VideoGstplayer] Using Gstreamer 1.2.1.0
[INFO              ] [Video       ] Provider: gstplayer
[INFO              ] [OSC         ] using <thread> for socket
[DEBUG             ] [Base        ] Create provider from mouse
[DEBUG             ] [Base        ] Create provider from wm_touch
[DEBUG             ] [Base        ] Create provider from wm_pen
[INFO              ] [Base        ] Start application main loop
[INFO              ] [GL          ] NPOT texture support is available
[WARNING           ] [Accordion   ] not enough space for displaying all children

[WARNING           ] [Accordion   ] need 132px, got 100px
[WARNING           ] [Accordion   ] layout aborted.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.GraphMenu object at 0x07106A40> have no cols or r
ows set, layout is not triggered.
[WARNING           ] [Accordion   ] not enough space for displaying all children

[WARNING           ] [Accordion   ] need 132px, got 100px
[WARNING           ] [Accordion   ] layout aborted.
[WARNING           ] <__main__.GraphMenu object at 0x07106A40> have no cols or r
ows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[WARNING           ] <__main__.UnnamedMenu object at 0x07106810> have no cols or
 rows set, layout is not triggered.
[ERROR             ] unable to access to <\hiberfil.sys>
Traceback (most recent call last):
  File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\uix\filech
ooser.py", line 122, in is_hidden
    return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
error: (32, 'GetFileAttributesEx', 'The process cannot access the file because i
t is being used by another process.')
[ERROR             ] unable to access to <\pagefile.sys>
Traceback (most recent call last):
  File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\uix\filech
ooser.py", line 122, in is_hidden
    return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
error: (32, 'GetFileAttributesEx', 'The process cannot access the file because i
t is being used by another process.')
[INFO              ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "D:\workspace\kivyWork\GUI\main.py", line 76, in <module>
     MainApp().run()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\app.py",
line 792, in run
     runTouchApp()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\base.py",
 line 481, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\core\wind
ow\window_pygame.py", line 381, in mainloop
     self._mainloop()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\core\wind
ow\window_pygame.py", line 287, in _mainloop
     EventLoop.idle()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\base.py",
 line 324, in idle
     self.dispatch_input()
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\base.py",
 line 309, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\base.py",
 line 275, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "_event.pyx", line 316, in kivy._event.EventDispatcher.dispatch (kivy\_e
vent.c:4409)
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\uix\behav
iors.py", line 110, in on_touch_up
     self.dispatch('on_release')
   File "_event.pyx", line 312, in kivy._event.EventDispatcher.dispatch (kivy\_e
vent.c:4359)
   File "C:\Users\My.Files\Downloads\Kivy-1.8.0-py2.7-win32\kivy\kivy\lang.py",
 line 1299, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "D:\workspace\kivyWork\GUI\main.kv", line 63, in <module>
     on_release: root.load(filechooser.path, filechooser.selection)
   File "D:\workspace\kivyWork\GUI\main.py", line 34, in load
     self.text_input.text = stream.read()
 AttributeError: 'NoneType' object has no attribute 'text'
Press any key to continue . . .

您从未设置 text_input 的值,因此它仍然是默认值 None。仅设置一个 id 不会设置该属性。这里有几个简单的修复。

首先,您可以填写那个值:

<OptionMenu>:
    # this sets the `text_input` attribute to the value
    # of the `text_input` id below
    text_input: text_input
    ...
    TextInput:
        id: text_input
    ...

或者,您可以通过 ids 集合访问小部件,并完全删除 text_input 属性:

def load(self, path, filename):
    with open(os.path.join(path, filename[0])) as stream:
        # self.ids is the collection of ids for this widget rule
        self.ids.text_input.text = stream.read()
    self.dismiss_popup()