【发布时间】:2019-07-21 09:45:47
【问题描述】:
我已经写了这段代码来实现打开文件时Listbox的数据,虽然有一个AttributeError,但我无法理解修复这个错误。
from Tkinter import *
import tkFileDialog
import csv
from imdb import IMDb
class STproject:
def __init__(self,app): #1
self.mlb=LabelFrame(app, text='Movie Recommendation Engine')
self.mlb.grid()
self.lframe3=LabelFrame(self.mlb,text="Movies/Users",background='purple')
self.lframe3.grid(row=0,column=1)
self.framebutton=Frame(self.mlb,background='pink',height=50,width=50)
self.framebutton.grid(row=0,column=0)
self.buttonsnlabels()
def buttonsnlabels(self):
self.ratingbutton=Button(self.framebutton,text='Upload Rating',command=lambda :self.file2())
self.ratingbutton.grid()
self.lb1 = Listbox(self.lframe3)
self.lb1.grid()
self.lb1.insert(self.emp2) //self.emp2 its locally ?
def file2(self):
umovies=tkFileDialog.askopenfilename()
f=open(umovies)
self.emp2=[]
self.csv_file2 = csv.reader(f)
for line2 in self.csv_file2:
self.emp2.append(line2[2])
root=Tk()
root.title()
application=STproject(root)
root.mainloop()
这里有完整的错误:
Traceback (most recent call last):
File "C:/Users/Umer Selmani/Desktop/voluntarily/Voluntiraly.py", line 846, in <module>
application=STproject(root)
File "C:/Users/Umer Selmani/Desktop/voluntarily/Voluntiraly.py", line 814, in __init__
self.a=self.emp2[1]
AttributeError: STproject instance has no attribute 'emp2'
【问题讨论】:
-
你可以使用
command=self.file2 -
.insert(self.emp2)在按钮创建后执行,而不是在用户单击按钮后执行。你必须在file2()中使用.insert(self.emp2)
标签: python python-2.7 tkinter