【问题标题】:Unable to add an image to a tkinter window无法将图像添加到 tkinter 窗口
【发布时间】: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'

【问题讨论】:

    标签: image tkinter png


    【解决方案1】:

    尝试:

    path = 'I:/python_work/TemplateRack_GUI.tif'
    

    没有打开 通过此更改,您的代码运行良好,尝试使用另一个 tif

    【讨论】:

    • 这样做会导致以下错误:OSError: decoder libtiff not available
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2021-03-06
    • 1970-01-01
    • 2019-09-06
    • 2022-01-18
    • 2013-01-28
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多