Search code examples
pythontkinter

Images in Tkinter Grid Layout Overlapping


I'm working on a Tkinter app that displays images of cats in a grid-like layout. My goal is to place the images alternately on the left and right sides of the window, stacking them vertiacaly. However, the images are not appearing in the expected positions.

Here is the code I'm using:

from PIL import Image, ImageTk
import tkinter as tk
import os
import gettingimg2 as img

img.get_titles_and_images("cats", 10)

root = tk.Tk()
root.title("Cat of the Day")
root.geometry("400x900")

images = []

y_position = 70
count = 0



def closing():
    count = 1
    while count < 11:
        os.remove("C:/Users/User/Desktop/cats/cutecat" + str(count) + ".jpg")
        count += 1
    root.destroy()
    
root.protocol("WM_DELETE_WINDOW", closing)

for i in range(10):

    xcount = 10
    swich = False
    if i == 1 or i == 3 or i == 5 or i == 7 or i == 9 :
        xcount = 200

    image_path = f"C:/Users/User/Desktop/cats/cutecat{i + 1}.jpg"
    image = Image.open(image_path)
    max_width, max_height = 190, 140
    image.thumbnail((max_width, max_height), Image.Resampling.LANCZOS)

    photo = ImageTk.PhotoImage(image)

    label = tk.Label(root, image=photo)

    label.place(x=xcount, y=y_position)
    if count == 2 or count == 4 or count == 6 or count == 8 or count == 10:
        y_position += 150

    images.append(photo)
    count += 1
    print("cutecat" + str(i + 1), "x =" + str(xcount), "y=" + str(y_position))

root.mainloop()

The images should alternate between the left (x=10) and right (x=200) sides, and the y_position should increase every second image to stack them vertically. However, the images are not displaying as expected.

Here are the logs from my program, which show the coordinates where each image is placed:

cutecat1 x=10 y=70
cutecat2 x=200 y=70
cutecat3 x=10 y=220
cutecat4 x=200 y=220
cutecat5 x=10 y=370
cutecat6 x=200 y=370
cutecat7 x=10 y=520
cutecat8 x=200 y=520
cutecat9 x=10 y=670
cutecat10 x=200 y=670

how it looks

all the cats + name

I expected each image to be placed at the correct x and y positions, alternating between the left and right sides and increasing the y position by 150 pixels every second image. I already tried making the window bigger.


Solution

  • My goal is to place the images alternately on the left and right sides of the window, stacking them vertically

    expected each image to be placed at the correct x and y positions, alternating >>between the left and right sides and increasing the y position by 150 pixels every >>second image

    The problem can be fixed.

    Online 45, change this:

    l = tk.Label(root, image=image)
                
    

    to:

    l = tk.Label(root, image=photo) #<== image-> photo
    
                
    

    On line 47, change this:

    if count == 2 or count == 4 or count == 6 or count == 8 or count == 10:
    

    To:

    if count == 1 or count == 3 or count == 5 or count == 7 or count == 9:
    

    Screenshot:

    enter image description here