簡體   English   中英

我想打開一個文件對話框並將所選文件的位置作為參數傳遞給 tkinter 以在 gui 上顯示?

[英]I want to open a file dialogue and pass the location of selected file as argument to tkinter to show on gui?

我面臨的問題是文件對話返回 _io.TextIOWrapper object 而 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()

正如對該問題的評論中所說,您想使用askopenfilename()而不是askopenfile() 或者, TextIOWrapper object 有一個名為name的變量,其中包含文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM