0

I´m trying to display a png image on a GUI created with Tkinter. To display the image I'm using Pillow, but something is missing that for now the image is not displayed on the window. Anyone know why it doesn´t work?

Code:

from tkinter import *
from PIL import ImageTk, Image

w = 650
p = 470


x = (w/2) - (w/2)
y = (w/2) - (p/2)

def fourthWindowFunction():

        fourthWindow = Tk()
        fourthWindow.title("TED")

        fourthWindowTitle = Label(fourthWindow, text="Resultados")
        fourthWindowTitle.grid(column=1, row=0, padx=10, pady=10)

        
        image = Image.open(r"c:\users\tomas\deformada.png")
        display = ImageTk.PhotoImage(image)

        generatePDF = Button(fourthWindow, text="Gerar Relatório PDF", command=None)
        generatePDF.grid(column=0, row=9, padx=10, pady=10)

        exit = Button(fourthWindow, text="Sair", command=quit)
        exit.grid(column=2, row=9, padx=10, pady=10)

        fourthWindow.geometry('%dx%d+%d+%d' % (w, p, x, y))
        fourthWindow.mainloop()

fourthWindowFunction()
amotoli
  • 39
  • 7

1 Answers1

0

You have created the image variable display which you then need to put onto a Label (or you could use a Canvas). I added the necessary lines to put your image on a Label. You will need to adjust the parameters of grid() to suit your needs because I'm not sure how you want it to be displayed/positioned.

from tkinter import *
from PIL import ImageTk, Image

w = 650
p = 470


x = (w/2) - (w/2)
y = (w/2) - (p/2)

def fourthWindowFunction():

        fourthWindow = Tk()
        fourthWindow.title("TED")

        fourthWindowTitle = Label(fourthWindow, text="Resultados")
        fourthWindowTitle.grid(column=1, row=0, padx=10, pady=10)

        
        image = Image.open(r"c:\users\tomas\deformada.png")
        display = ImageTk.PhotoImage(image)
        image_label = Label(fourthWindow, image=display)
        image_label.grid()

        generatePDF = Button(fourthWindow, text="Gerar Relatório PDF", command=None)
        generatePDF.grid(column=0, row=9, padx=10, pady=10)

        exit = Button(fourthWindow, text="Sair", command=quit)
        exit.grid(column=2, row=9, padx=10, pady=10)

        fourthWindow.geometry('%dx%d+%d+%d' % (w, p, x, y))
        fourthWindow.mainloop()

fourthWindowFunction()
Rory
  • 661
  • 1
  • 6
  • 15
  • Is it possible to resize the scale of the image? – amotoli Jul 06 '22 at 20:59
  • Yes, you can do that with the `PIL` module also. If you know the size you want you can call the resize like [this example](https://stackoverflow.com/questions/37631611/python-how-to-resize-an-image-using-pil-module). Or if you need to maintain aspect ratio [look at this example](https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio) – Rory Jul 06 '22 at 21:06