-1
from tkinter import *
from PIL import ImageTk, Image

root = Tk()
root.title("Images")
root.iconbitmap(r"C:\Users\DellABD\Downloads\lolol.ico")


my_img1 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\illusion.jpg"))
my_img2 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0053.jpg"))
my_img3 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0054.jpg"))
my_img4 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0055.jpg"))
my_img5 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0056.jpg"))

my_img_list = [my_img1, my_img2, my_img3, my_img4, my_img5]

my_label = Label(image = my_img1)
my_label.grid(row = 0, column = 0, columnspan = 3)


def forward(image_number):
    global my_label
    global back
    global forward

    my_label.grid_forget()
    my_label = Label(my_img_list[image_number - 1])

    my_label.grid(row = 0, column = 0, columnspan = 3)

def back():
    global my_label
    global back
    global forward

    global my_label
    global back
    global forward

    my_label.grid_forget()
    my_label = Label(my_img_list[image_number - 1])

    button_forward = Button(root, text = ">>>", command = lambda: image_number + 1)
    button_back = Button(root, text = "<<<", command = lambda: image_number - 1)

    if button_back == 1:
        button_back = Buuton(rrot, text = "<<<", state = DISABLED)

    button_back.grid(row = 1, column = 0)
    button_forward.grid(row = 1, column = 2)
    my_label.grid(row = 0, column = 0, columnspan = 3)



button_back = Button(root, text = "<<<", padx = 20, pady = 20, command = back)
button_forward = Button(root, text = ">>>", padx = 20, pady = 20, command = lambda: forward(2))
button_quit = Button(root, text = "Exit", padx = 20, pady = 20, command = root.quit)

button_back.grid(row = 1, column = 0)
button_forward.grid(row = 1, column = 2)
button_quit.grid(row = 1, column = 1)

root.mainloop()

when I run the programme and press the Forward button it shows me an error. I wanted to move on next image by pressing forward and when I press the first image disappears but next image dont show up and shows error.

if master._last_child_ids is None: AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'

Matrix
  • 7
  • 2

1 Answers1

0

The problem is this code:

Label(my_img_list[image_number - 1])

You are trying to use an image as the parent/master of a label. You need to use this instead:

Label(image=my_img_list[image_number - 1])
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tried and it gives me another big error thing. The image didnt came up correctly and all buttons disappeared. – Matrix Jun 28 '20 at 09:22
  • @Matrix: when I make the change I suggest (it has to be made in two places), the images show and the error from your question goes away. You may have other bugs in your code, but this answer absolutely explains the error you gave in the question. – Bryan Oakley Jun 28 '20 at 13:07