2017-06-20 9 views
0

私は、各要素についてさまざまな情報を表示する化学GUIを作成しようとしています。クラスインスタンスのリストを使用して情報を出力していますが、引き続き'list' object has no attribute 'atomic_number'を取得しています。これは私が設定したクラスで、エラーを出すコードと一緒になっています。Python:クラスインスタンス変数のリストを検索できません

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の

答えて

1

上の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ステートメントは、リストの各要素を変数に割り当てます。その要素はカスタムクラスのインスタンスであり、そこでは属性を操作することができます。

+0

私のインターネット接続は現時点では非常に遅いので、家に帰るまでは何もテストすることができませんが、これは私がそれを見ているという意味になります。ありがとう! – Goalieman

関連する問題