-1

The problem is my window is not showing any images that I want on the Tkinter window below is my code which I tried but some problems are occurring. the image folder is in the same folder in which the python file is and also some =thing with fp was coming up while trying to print jpg image. can someone explain what that is

from Tkinter import *
import os
from PIL import Image, ImageTk

gallery_root = Tk()
gallery_root.geometry("738x638")

for (root, dirs, files) in os.walk("/img"):
   for file in files:
      if file.endwith(".png"):
          Label(image=PhotoImage(file=str(file))).pack()
      if file.endwith(".jpg"):
          Label(image=ImageTk.PhotoImage(Image.open()))
gallery_root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
sparsh
  • 135
  • 1
  • 1
  • 11
  • This code won't run due to errors in the code - you have `endwith` rather than `endswith`, and you have a missing parameter with the call to `open`. You're also using an unsupported version of Python. – Bryan Oakley Sep 08 '21 at 21:21

1 Answers1

1

Don't use generic from tkinter import *, but rather specify explicitly what you are using.

For image display, this code should work:

image = ImageTk.PhotoImage(Image.open(filepath))
label = Label(image=image)
label.image = image
label.pack()

with filepath = os.path.join(root, file) for full path instead of file which is only filename.

Moreover, you make a lot of mistakes... Have you tried your code ? For instance, endwith don't exist. Use endswith instead. And is your path really "/img"? Shouldn't it be "./img"?

Kaz
  • 1,047
  • 8
  • 18