为什么我无法将变量设置为none

时间:2016-03-05 21:49:08

标签: python kivy

好的,我正在尝试将变量设置为None,以便我可以清除它,但每次执行此操作时,都会收到错误AttributeError: 'NoneType' object has no attribute 'replace'。我也在使用Kivy以防万一。代码如下:

if self.ids.txt_inpt1.text != '':
    angA = int(self.ids.txt_inpt1.text)
    print angA
else:
    angA = None

感谢任何帮助。

回溯

Traceback (most recent call last):
File "solve_triangle.py", line 183, in <module>
 mainApp().run()
File "/Library/Python/2.7/site-packages/kivy/app.py", line 828, in run
 runTouchApp()
File "/Library/Python/2.7/site-packages/kivy/base.py", line 487, in runTouchApp
 EventLoop.window.mainloop()
File "/Library/Python/2.7/site-packages/kivy/core/window/window_pygame.py", line 403, in mainloop
 self._mainloop()
File "/Library/Python/2.7/site-packages/kivy/core/window/window_pygame.py", line 290, in _mainloop
 EventLoop.idle()
File "/Library/Python/2.7/site-packages/kivy/base.py", line 330, in idle
 self.dispatch_input()
File "/Library/Python/2.7/site-packages/kivy/base.py", line 315, in dispatch_input
 post_dispatch_input(*pop(0))
File "/Library/Python/2.7/site-packages/kivy/base.py", line 221, in post_dispatch_input
 listener.dispatch('on_motion', etype, me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7699)
File "/Library/Python/2.7/site-packages/kivy/core/window/__init__.py", line 1030, in on_motion
 self.dispatch('on_touch_down', me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7699)
File "/Library/Python/2.7/site-packages/kivy/core/window/__init__.py", line 1046, in on_touch_down
 if w.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7699)
File "/Library/Python/2.7/site-packages/kivy/uix/widget.py", line 432, in on_touch_down
 if child.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7699)
File "/Library/Python/2.7/site-packages/kivy/uix/widget.py", line 432, in on_touch_down
 if child.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7699)
File "/Library/Python/2.7/site-packages/kivy/uix/behaviors/button.py", line 110, in on_touch_down
 self.dispatch('on_press')
File "kivy/_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:7654)
File "kivy/_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:13497)
File "kivy/_event.pyx", line 1108, in kivy._event.EventObservers._dispatch (/private/tmp/pip-build-jshW4M/kivy/kivy/_event.c:12329)
File "/Library/Python/2.7/site-packages/kivy/lang.py", line 1557, in custom_callback
 exec(__kvlang__.co_value, idmap)
File "calculator.kv", line 61, in <module>
 on_press: root.solve_triangle()
File "solve_triangle.py", line 169, in solve_triangle
 self.ids.txt_inpt1.text = angA
File "kivy/weakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (/private/tmp/pip-build-jshW4M/kivy/kivy/weakproxy.c:1235)
File "kivy/properties.pyx", line 408, in kivy.properties.Property.__set__ (/private/tmp/pip-build-jshW4M/kivy/kivy/properties.c:5114)
File "kivy/properties.pyx", line 1390, in kivy.properties.AliasProperty.set (/private/tmp/pip-build-jshW4M/kivy/kivy/properties.c:23025)
File "/Library/Python/2.7/site-packages/kivy/uix/textinput.py", line 2849, in _set_text
 text = text.replace(u'\r\n', u'\n')
AttributeError: 'NoneType' object has no attribute 'replace'

1 个答案:

答案 0 :(得分:0)

此处发生错误:

File "solve_triangle.py", line 169, in solve_triangle
 self.ids.txt_inpt1.text = angA

当您将angA设置为None然后分配给self.ids.txt_inpt1.text时,kivy代码会爆炸,因为它假定text属性始终< / em>是一个字符串,而不是None

如果您必须将angA分配给text,并且angA可以取无值,请尝试以下操作:

self.ids.txt_input1.text = angA if angA is not None else ''