【发布时间】:2018-12-23 02:59:03
【问题描述】:
我正在尝试将图像添加到我的 tkinter GUI。但是我不断收到错误,特别是我的图像没有“读取”属性。我已经尝试过使用 TIF、GIF 和 PNG 格式的文件,但似乎都没有。我已经检查过,其他似乎没有问题添加 png 格式的图像。我感觉问题很明显,但我想念它。
import tkinter as tk
from PIL import ImageTk, Image
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Lifespan Data Analyser")
self.pack(fill=tk.BOTH, expand=1)
path = Image.open('I:/python_work/TemplateRack_GUI.tif')
img = ImageTk.PhotoImage(file = path) #problem is here I think
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
panel.image=img
root = tk.Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
收到此属性错误:
AttributeError: 'TiffImageFile' 对象没有属性 'read'
AttributeError: 'JpegImageFile' 对象没有属性 'read'
【问题讨论】: