Python:无法搜索类实例变量列表



我正在尝试创建一个化学GUI,显示有关每个元素的各种信息。我正在使用类实例列表来打印信息,但我继续得到一个'list' object has no attribute 'atomic_number'.这是我设置的类,以及给我错误的代码。

class ElementInformation(object):
def __init__(self, atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point)
self.atomic_number = atomic_number
self.element_name = element_name
self.element_symbol = element_symbol
self.atomic_weight = atomic_weight
self.melting_point = melting_point
self.boiling_point = boiling_point
def find_element():
update_status_label(element_information, text_entry)  
# text entry is a text entry field in TKinter
# other code in here as well (not part of my question

def update_status_label(element_instances, text_input):
for text_box in element_instances.atomic_number:
if text_input not in text_box:
# do stuff
else:
pass
element_result_list = [*results parsed from webpage here*]
row_index = 0
while row_index < len(element_result_list):
element_instances.append(ElementInformation(atomic_number, element_name, element_symbol, atomic_weight, melting_point, boiling_point))
# the above information is changed to provide me the correct information, it is just dummy code here
row_index += 1

我的问题出在函数update_status label,特别是for循环。Python 抛出一个错误(就像我之前说的),上面写着'list' object has no attribute 'atomic_number'.对于我的生活,我似乎无法弄清楚出了什么问题。感谢您的任何帮助!

如果有任何区别,我在Windows上使用Python 3.x。

试试这个:

for element in element_instances:
text_box = element.atomic_number:
if text_input not in text_box:
# do stuff
else:
pass

列表"element_instances"是一个 Python 列表。它没有属性".atomic number",即使它中的所有元素都有这样的属性。Python 的for语句将列表的每个元素分配给一个变量 - 该元素是自定义类的一个实例,您可以在其中摆弄属性。

最新更新