为啥我无法将变量设置为无 [关闭]

Posted

技术标签:

【中文标题】为啥我无法将变量设置为无 [关闭]【英文标题】:Why am I not able to set a variable as none [closed]为什么我无法将变量设置为无 [关闭] 【发布时间】:2016-06-19 14:54:51 【问题描述】:

好的,所以我尝试将变量设置为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'

【问题讨论】:

请给minimal reproducible example;看在上帝的份上,您发布的代码甚至从未提及 .replace 请将回溯放到您的问题中。该代码不会直接导致问题;我们需要更多信息。 你的代码中可能有angA.replace,而angA == None的情况就是这样。 来吧人们,无需关闭此问题,回溯中有大量信息,现在OP已将其添加到问题中。还有更糟糕的问题,比如这个:***.com/questions/35834085/failed-to-parse-error 【参考方案1】:

错误发生在这里:

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

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

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

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

【讨论】:

或者重构你的代码,如果不是所有的值都有效,就不能按下按钮来“解决根三角形”。

以上是关于为啥我无法将变量设置为无 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

将测试目标的“主机应用程序”设置为无,然后无法找到捆绑资源

PHP 为啥 $_SESSION 变量拒绝在浏览器关闭时取消设置?

将表单(添加)对话框设置为无模式

即使指针事件设置为无,也使元素可点击

为啥我的数据无法插入数据库? [关闭]

如果我将默认设置为无,我可以省略 Optional 吗?