python - Filedialogue error in Tkinter -
question tkinter :
i want create browse along text display display file pick browse button. following code :
edit 1.
button_opt = {'fill': tkconstants.both, 'padx': 5, 'pady': 5} tkinter.button(self, text='browse , open filename - manually upload it', command=self.askopenfilename).pack(**button_opt) self.file_opt = options = {} options['defaultextension'] = '.txt' options['filetypes'] = [('all files', '.*'), ('text files', '.txt')] options['initialdir'] = 'c:\\' options['initialfile'] = 'myfile.txt' options['parent'] = root options['title'] = 'browse' self.dir_opt = options = {} options['initialdir'] = 'c:\\' options['mustexist'] = false options['parent'] = root options['title'] = 'browse' image = image.open("/home/kuber/downloads/godata/images/header.png") photo = imagetk.photoimage(image) label = label(image=photo) label.image = photo # keep reference! label.place(width=768, height=576) label.pack(side = top) self.centerwindow() self.master.columnconfigure(10, weight=1) #tkinter.button(self, text='upload file', command=self.fname).pack(**button_opt) self.file_name = text(self, width=39, height=1, wrap=word) def fname(self): self.file_name = text(self, width=39, height=1, wrap=word) self.file_name.grid(row=1, column=1, columnspan=4, sticky=w) def askopenfilename(self): # filename filename = tkfiledialog.askopenfilename(**self.file_opt) # open file on own if filename: open(self.filename, 'r') inp_file: print "1" self.file_name.delete(0.0, end) self.file_name.insert(0.0, inp_file.read()) #return open(filename, 'r') as press button browse , open file. expect go askopenfilename function text widget. error :
attributeerror: tkfiledialogexample instance has no attribute 'filename'
also when include self.file_name.grid(row=1, column=1, columnspan=4, sticky=w) outside fname, tkinter hangs. pointers ?
when see python error `x instance has no attribute 'filename', means says. root cause 1 of 2 things:
- x object intended use, , indeed not have attributev(maybe misspelled it, o forgot define it), or
- you told program use x, x not think s (ie: think refers object of particular type it's string or number or other class.
so, ask yourself, "why doesn't tkfiledialogexample have attribute? did define have attribute? , when? did misspell it? or, code supposed attribute other object?
in other words, code using self.filename: self think is?
Comments
Post a Comment