0
I want to add an image to the widget, but nothing appears, I tried to follow the same face https://stackoverflow.com/questions/28139637/how-can-i-display-an-image-using-pillow , but it doesn’t work.
His code:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
tkimage = ImageTk.PhotoImage(Image.open("bola.jpg"))
tk.Label(root, image=tkimage).pack()
root.mainloop()
image in his code works:
My code:
from tkinter import *
from PIL import Image, ImageTk
class TesteImage1:
def __init__(self, master=None):
self.widget1 = Frame(master)
self.widget1.pack()
self.imagem = Label(self.widget1)
# imagem = ImageTk.PhotoImage(Image.open("bola.jpg"))
self.imagem['image'] = ImageTk.PhotoImage(Image.open("bola.jpg"))
# self.imagem['text'] = "Testando"
self.imagem.pack()
self.teste = Label(self.widget1, text="testando")
self.teste.pack()
root = Tk()
TesteImage1(root)
root.mainloop()
Elton’s changes work better, because I will make the image dynamic, that is, a function will change the image. def changeImage(self): width = 500 height = 500 sample = Image.open("ball.jpg") self.sample = Imagetk.Photoimage(sample width, height), Image.ANTIALIAS)) self.image['image'] = self.sample
– WesleyRodrigues