我正在尝试为图像生成器创建Tkinter GUI,一旦用户点击“生成图像”按钮,它就会在窗口上显示“diff-overlay.jpg”。请注意,此文件位于同一目录中。
我创建了方法(命令)'generate'来在窗口上显示图像。但是“生成图像”按钮没有显示图像(diff-overlay.jpg)。有人可以帮我解决我在哪里出错吗?谢谢!
import tkinter as tk
import numpy as np
from PIL import ImageTk,Image #for image in tk
class HeatMap(tk.Tk):
def __init__(self, parent=0):
self.mainWindow = Frame(parent)
self.fTop = Frame(self.mainWindow)
self.fTop.pack(fill="both",expand =True)
self.button = Button(self.fTop, text='Generate Image',command=self.generate)
self.button.pack(side="right")
self.mainWindow.pack(fill="both")
#this should display the image on the root window
def generate(self):
image = Image.open('diff-overlay.jpg')
photo = ImageTk.PhotoImage(image)
w1 = tk.Label(root, image=photo).pack(side="right",expand=True)
root = Tk()
root.title("Image Generator")
root.geometry("1200x1200")
app=HeatMap(root)
root.mainloop()
答案 0 :(得分:1)
我就是这样做的:
def iniImage():
img = Image.open("image.png")
img = img.resize((300,200), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
panel = Label(window, image=img)
panel.image = img
panel.place(relheight=.095,relwidth=0.25,relx=0.7,rely=0.03)
为了能够使用.jpg图像,我认为你必须使用类似的东西:
path = "image.jpg"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img = ImageTk.PhotoImage(Image.open(path))
答案 1 :(得分:0)
我能够通过存储图像对象来解决它。
self.iconPath = 'diff-overlay.jpg'
self.icon = ImageTk.PhotoImage(Image.open(self.iconPath))
self.icon_size = Label(root)
self.icon_size.image = self.icon
self.icon_size.configure(image=self.icon)
self.icon_size.pack(side=LEFT,expand=True)