2017-04-07 15 views
0

未知語を管理するためのGUIプログラムであるボキャブラリを作成しています。私は得ています:AttributeError: 'ボキャブラリ'オブジェクトに 'listBox'という属性がありません

/usr/bin/python3.5 /home/cali/PycharmProjects/Vocabulary/Vocabulary.py Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/init.py", line 1553, in call return self.func(*args) File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 86, in add_item self.listBox.insert(END, self.get_word()) AttributeError: 'Vocabulary' object has no attribute 'listBox'

Process finished with exit code 0

...リストボックスに項目を追加しようとしました。ここで

は、私がやっていることです:私は、Python 3.5を使用してい

#!/usr/bin/env python 

# Vocabulary.py 
# GUI program to manage unknown words 

from tkinter import * 

class Word: 

    def __init__(self, wordorphrase, explanation, translation, example): 
     self.wordorphrase = wordorphrase 
     self.explanation = explanation 
     self.translation = translation 
     self.example = example 

class Vocabulary(Frame): 

    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.master = master 
     self.master.resizable(width = False, height = False) 
     self.master.title("Vocabulary") 
     self.create_widgets() 

    def create_widgets(self): 

     lblWordsOrPhrases = Label(self.master, text = 'Words or Phrases:') 
     lblWordsOrPhrases.grid(row = 0, column = 0) 

     lblWordOrPhrase = Label(self.master, text = 'Word or phrase:') 
     lblWordOrPhrase.grid(row = 0, column = 1, sticky = W) 

     listBox = Listbox(self.master, 
          height = 34, 
          width = 30) 
     listBox.grid(row = 1, column = 0, rowspan = 7) 

     txt_WordOrPhrase = Text(self.master, 
           height = 1, 
           width = 40) 
     txt_WordOrPhrase.grid(row = 1, column = 1, sticky = N) 

     lblExplanation = Label(self.master, text = 'Explanation:') 
     lblExplanation.grid(row = 2, column = 1, sticky = W) 

     txt_Explanation = Text(self.master, 
           height = 10, 
           width = 40) 
     txt_Explanation.grid(row = 3, column = 1, sticky = N) 

     lblTranslation = Label(self.master, text = 'Translation:') 
     lblTranslation.grid(row = 4, column = 1, sticky = W) 

     txt_Explanation = Text(self.master, 
           height = 10, 
           width = 40) 
     txt_Explanation.grid(row = 5, column = 1, sticky = N) 


     lblExamples = Label(self.master, text = 'Example(s):') 
     lblExamples.grid(row = 6, column = 1, sticky = W) 

     txt_Explanation = Text(self.master, 
           height = 10, 
           width = 40) 
     txt_Explanation.grid(row = 7, column = 1, sticky = S) 

     btn_Add = Button(self.master, 
         text = 'Add', 
         command = self.add_item) 
     btn_Add.grid(row = 8, column = 0, sticky = W) 

    def get_word(self): 
     return self.txt_WordOrPhrase.get('1.0', '1.0 lineend') 

    def get_explanation(self): 
     return self.txt_Explanation.get('1.0', '1.0 lineend') 

    def get_translation(self): 
     return self.txt_Translation.get('1.0', '1.0 lineend') 

    def get_example(self): 
     return self.txt_Example.get('1.0', '1.0 lineend') 

    def add_item(self): 
     self.listBox.insert(END, self.get_word()) 

def main(): 
    root = Tk() 
    Vocabulary(root) 
    root.mainloop() 

if __name__ == '__main__': 
    main() 

答えて

2

listboxselfで設定されていないため、create_widgetsのローカル変数です。変数をインスタンス全体で使用できるようにするには、変数をselfに含める必要があります。

この変更を適用するには、の行をself.listBox = Listbox(self.master, height = 34, width = 30)に変更し、すべての参照をlistBoxに変更してself.listBoxに変更してください。

self.listBox__init__()に定義すると、インスタンス変数の追跡に役立つ場合があります。

+0

ありがとう、私はそれ以降も他の問題があったので、create_widgets()内のすべてのものの前に自己を追加しました。それは今働く。 –

+0

素晴らしい!インスタンス変数をたくさん追加しているので、トラックを失わないように '__init __()'でそれらを管理することをお勧めします。あなたの質問は必ず解決済みとマークしてください! –

+0

__init __()のインスタンス変数の管理については、まだ私が初心者であるため、さらに説明できますか? –

関連する問題