Reputation:
I am experiencing some issues when displaying the location of the image selected. Is there a reason why it displays <_io.TextIOWrapper name =
along with mode='r'encoding ='cp1252>
? I just want it to display the location of the image along with the name of the image not those extra stuff. Is there something that I am doing that is causing this to occur? Please advise.
def button(self):
self.button = ttk.Button(self.labelFrame, text = "Upload Image", command = self.fileDialog)
self.button.grid(column = 1, row = 1)
def fileDialog(self):
self.filename = filedialog.askopenfile(initialdir = "/", title = "Select a File", filetype = (("jpeg", "*.jpg"), ("All files", "*.")))
self.label = ttk.Label(self.labelFrame, text = "")
self.label.grid(column = 1, row = 2)
self.label.configure(text = self.filename)
Upvotes: 1
Views: 175
Reputation: 142631
filedialog.askopenfile
gives file object, not file name.
You have to display self.filename.name
instead of self.filename
Full working example
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
file_object = filedialog.askopenfile(title="Select file")
print('file_object:', file_object)
print('file_object.name:', file_object.name)
#data = file_object.read()
label = tk.Label(root, text=file_object.name)
label.pack()
root.mainloop()
Or use askopenfilename
instead of askopenfile
and you get file name.
Full working example
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
filename = filedialog.askopenfilename(title="Select file")
print('filename:', filename)
#data = open(filename).read()
label = tk.Label(root, text=filename)
label.pack()
root.mainloop()
Upvotes: 1