配置 tkinter 小部件时出错:“NoneType"对象没有属性 [英] Error when configuring tkinter widget: 'NoneType' object has no attribute
问题描述
我正在运行下面的代码,当我对值进行硬编码时运行良好
I am running the below code which run fine when I hard code the value
from nsetools import Nse
nse = Nse()
with open('all_nse_stocks') as nse_stocks:
for stock in nse_stocks:
q = nse.get_quote('INFY')
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
看到我对值 nse.get_quote('INFY') 进行了硬编码但是当我运行以下代码时,出现以下错误:
see that I have hard-coded the value nse.get_quote('INFY') But when I run the following code, I get the following error:
from nsetools import Nse
nse = Nse()
with open('all_nse_stocks') as nse_stocks:
for stock in nse_stocks:
q = nse.get_quote(stock)
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
错误:
Traceback (most recent call last):
File "test.py", line 6, in <module>
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
AttributeError: 'NoneType' object has no attribute 'get'
请帮忙
推荐答案
NoneType object has no attribute ...
表示您有一个 None
的对象,并且您正在尝试使用该对象的属性.
NoneType object has no attribute ...
means that you have an object that is None
, and you're trying to use an attribute of that object.
在您的情况下,您正在执行 q.get(...)
,因此 q
必须为 None
.由于 q
是调用 nse.get_quote(...)
的结果,所以该函数必须有可能返回 None
.您需要调整您的代码以应对这种可能性,例如在尝试使用之前检查结果:
In your case you're doing q.get(...)
, so q
must be None
. Since q
is the result of calling nse.get_quote(...)
, that function must have the possibility of returning None
. You'll need to adjust your code to account for that possibility, such as checking the result before trying to use it:
q = nse.get_quote(stock)
if q is not None:
print ...
问题的根源可能在于您阅读文件的方式.stock
将包含换行符,因此您应该在调用 nse.get_quote
之前将其去掉:
The root of the problem is likely in how you're reading the file. stock
will include the newline, so you should strip that off before calling nse.get_quote
:
q = nse.get_quote(stock.strip())
这篇关于配置 tkinter 小部件时出错:“NoneType"对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!