我面临的问题是,文件对话返回的是_io.TextIOWrapper对象,而tkinter将文件的位置作为字符串。
我尝试了所有方法将此_io.TextIOWrapper转换为字符串,但都失败了。
帮帮我!!
import tkinter as tk
import tkinter.filedialog
from PIL import Image, ImageTk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.main = tk.Frame(self)
self.main.grid(row=0, column=0)
dial = tkinter.filedialog.askopenfile()
self.disp_img = Image.open(dial).resize((400, 400))
self.disp_img = ImageTk.PhotoImage(self.disp_img)
self.img_label = tk.Label(self.main, image=self.disp_img)
self.img_label.grid(row=0, column=0)
if __name__ == '__main__':
app = App()
app.withdraw()
app.mainloop()
发布于 2020-07-02 04:01:50
正如在对该问题的评论中所说的,您希望使用askopenfilename()
而不是askopenfile()
。或者,TextIOWrapper
对象有一个名为name
的变量,其中包含文件名。
https://stackoverflow.com/questions/62684235
复制相似问题