属性错误: 'Vocabulary'对象没有属性'listBox'



我正在创建词汇表,一个用于管理未知单词的GUI程序。我得到:

/

usr/bin/python3.5/home/cali/PycharmProjects/Vocabulary/Vocabulary.py Tkinter 回调回溯中的异常(最近一次调用(:
文件 "/usr/lib/python3.5/tkinter/init.py",第 1553 行,在调用中 return self.func(*args( File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", 第 86 行, in add_item self.listBox.insert(END, self.get_word((( AttributeError: 'Vocabulary' 对象没有属性 'listBox'

进程已完成,退出代码为 0

。当我尝试将项目添加到列表框时。

这是我所做的:

#!/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()

我正在使用Python 3.5。

listboxcreate_widgets的局部变量,因为它没有设置self。为了使变量在实例范围内可用,您需要将其包含在 self 中。

create_widgets中的行更改为self.listBox = Listbox(self.master, height = 34, width = 30),并将每个对listBox引用更改为self.listBox以应用此更改。

您可能希望在 __init__() 中定义self.listBox,因为它可能有助于跟踪实例变量。

最新更新