【问题标题】:AttributeError: 'Vocabulary' object has no attribute 'listBox'AttributeError:“词汇”对象没有属性“列表框”
【发布时间】:2017-09-03 03:06:54
【问题描述】:

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

/usr/bin/python3.5 /home/cali/PycharmProjects/Vocabulary/Vocabulary.py Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
call 中的文件“/usr/lib/python3.5/tkinter/init.py”,第 1553 行 返回 self.func(*args) 文件“/home/cali/PycharmProjects/Vocabulary/Vocabulary.py”,第 86 行,在 添加项目 self.listBox.insert(END, self.get_word()) AttributeError: 'Vocabulary' object has no attribute '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。

【问题讨论】:

    标签: python tkinter listbox


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢,我在 create_widgets() 中的所有内容前面都添加了 self,因为在那之后我也遇到了其他问题。现在可以了。
    • 太棒了!由于您要添加很多实例变量,我建议您在__init__() 中管理它们,以免丢失轨道。请务必将您的问题标记为已解决!
    • 谈到在 __init__() 中管理实例变量,由于我还是个新手,能否进一步解释一下。
    • 查看下面的 StackOverflow 问题,了解更多详情! stackoverflow.com/questions/20661448/…
    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 2018-01-16
    • 2016-05-14
    • 2016-12-21
    • 2022-01-23
    • 2022-01-23
    • 2019-11-01
    • 2021-08-23
    相关资源
    最近更新 更多