2017-04-07 1 views
0

알 수없는 단어를 관리하는 GUI 프로그램 인 Vocabulary를 만들고 있습니다. 나는 다음과 같이 나타납니다 :AttributeError : 'Vocabulary'객체에 '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

... 목록 상자에 항목을 추가하려고합니다. 여기

내가 한 일이다 : 나는 파이썬 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에 변수를 포함해야합니다.

이 변경 사항을 적용하려면 create_widgets의 줄을 self.listBox = Listbox(self.master, height = 34, width = 30)으로 변경하고 모든 참조를 listBox에서 self.listBox으로 변경하십시오.

self.listBox__init__()에 정의하면 인스턴스 변수를 추적하는 데 도움이 될 수 있습니다.

+0

대단히 고맙습니다. 이후에도 다른 문제가 있었으므로 create_widgets()의 모든 항목 앞에 자신을 추가했습니다. 이제 작동합니다. –

+0

좋아요! 많은 인스턴스 변수를 추가하고 있기 때문에, 궤도를 잃지 않기 위해'__init __()'에서 그것들을 관리 할 것을 제안합니다. 질문을 해결 된 것으로 표시하십시오! –

+0

__init __()에서 인스턴스 변수를 관리 할 때, 아직 초보자이므로 더 설명 할 수 있습니까? –

관련 문제