Question about Tkinter :
I want to create a browse along with a text display which will display the file that I pick from the browse button. Following is my code :
Edit 1.
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
Tkinter.Button(self, text='Browse and open filename - then 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 a 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):
# get filename
filename = tkFileDialog.askopenfilename(**self.file_opt)
# open file on your own
if filename:
with open(self.filename, 'r') as 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 I press the button for Browse and open file. I expect to go from askopenfilename function to the text widget. But I get the error :
AttributeError: TkFileDialogExample instance has no attribute 'filename'
Also when I include self.file_name.grid(row=1, column=1, columnspan=4, sticky=W) outside Fname, Tkinter hangs.
Textwidget. See effbot.org/tkinterbook/text.htm and tkdocs.com/tutorial/text.html - Bryan Oakley