Python 属性错误:';词汇';对象没有属性';列表框';
我正在创建词汇表,一个GUI程序来管理未知单词。我得到: /usr/bin/python3.5/home/cali/pycharm项目/词汇表/词汇表.py Tkinter回调回溯中出现异常(最近一次调用最后一次):Python 属性错误:';词汇';对象没有属性';列表框';,python,tkinter,listbox,Python,Tkinter,Listbox,我正在创建词汇表,一个GUI程序来管理未知单词。我得到: /usr/bin/python3.5/home/cali/pycharm项目/词汇表/词汇表.py Tkinter回调回溯中出现异常(最近一次调用最后一次): 文件“/usr/lib/python3.5/tkinter/init.py”,第1553行,在调用中 返回self.func(*args)文件“/home/cali/PycharmProjects/词汇表/词汇表.py”,第86行,在 添加项目 self.listBox.inser
文件“/usr/lib/python3.5/tkinter/init.py”,第1553行,在调用中 返回self.func(*args)文件“/home/cali/PycharmProjects/词汇表/词汇表.py”,第86行,在 添加项目 self.listBox.insert(END,self.get_word())AttributeError:“词汇表”对象没有属性“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。您的
列表框
是创建小部件
的本地变量,因为它没有设置self
。为了使变量在实例范围内可用,您需要将其包含在self
中
将create_widgets
中的行更改为self.listBox=listBox(self.master,高度=34,宽度=30)
并将对listBox
的每个引用更改为self.listBox
,以应用此更改
您可能希望在
中定义self.listBox
,因为它可能有助于跟踪实例变量。您listBox
是创建小部件的局部变量,因为它没有设置self
。为了使变量在实例范围内可用,您需要将其包含在self
中
将create_widgets
中的行更改为self.listBox=listBox(self.master,高度=34,宽度=30)
并将对listBox
的每个引用更改为self.listBox
,以应用此更改
您可能希望在中定义self.listBox
,因为它可能有助于跟踪实例变量。非常感谢,我在create\u widgets()中的所有内容前面添加了self,因为之后我还遇到了其他问题。现在可以了,太好了!由于您正在添加许多实例变量,我建议您在\uuuu init\uuuu()
中对它们进行管理,以免迷失方向。确保将您的问题标记为已解决!当涉及到在uu init_uuu()中管理实例变量时,您能进一步解释我吗,因为我还是一个新手。请查看下面的StackOverflow问题以了解有关该问题的详细信息!非常感谢,我在create_widgets()中的所有内容前面添加了self,因为之后我还有其他问题。现在可以了,太好了!由于您正在添加许多实例变量,我建议您在\uuuu init\uuuu()
中对它们进行管理,以免迷失方向。确保将您的问题标记为已解决!当涉及到在uu init_uuu()中管理实例变量时,您能进一步解释我吗,因为我还是一个新手。请查看下面的StackOverflow问题以了解有关该问题的详细信息!