1

it's my first question here.

I use tkinter with python3.9 for displaying an image, a flag of which country it shows up on the window

# gets the img flag
flag = PhotoImage(file=flagsFolder+countryList[rand_label]) # the path is "D:\Images\Flags of the world" + "XXXX.gif"

# creates the img Label
flagLabel = Label(frame, image=flag)
flagLabel.place(x=window_width/2, y=200, anchor='center')

So, when I try to display the image, it shows me this:

this

A flag should be here instead of the white image.

Why isn't it showing up?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

So I messed around a bit and found out a solution.

I had to use ImageTk from Pillow so:

from PIL import ImageTk

Then, instead of creating a Label for my image, I made a Canvas:

# notice how I used ImageTk.PhotoImage instead of the basic PhotoImage from Tkinter
img = ImageTk.PhotoImage(file='filename.png')

# creates the img Canvas
imgCanvas = Canvas(frame)
imgCanvas.place(x=window_width/2, y=200, anchor='center', width=img.width(), height=img.height())
imgCanvas.create_image(0, 0, anchor=NW, image=img)

imgCanvas.image = img # now the image displays perfectly :D

The final result: