0
from tkinter import *
from PIL import ImageTk
import glob

def selection():
    global picTk
    picTk = Toplevel()
    picTk.title('Photos')
    picTk.geometry('800x600')
    global photoimage_list
    photoimage_list = []
    for filepath in glob.glob('photos/*.gif'):
        im = Image.open(filepath)
        pic = ImageTk.PhotoImage(image=im)
        photoimage_list.append(pic)

    global lb1
    global back_btn
    global exit_btn
    global for_btn

    n = 0
    img_show = photoimage_list[n]
    lb1 = Label(picTk,image=img_show)
    lb1.image = img_show
    lb1.place(x=10,y=10)

    back_btn = Button(picTk,text='<<',command=lambda:back(n),state=DISABLED)
    back_btn.place(x=50,y=550)

    exit_btn = Button(picTk,text='Exit',command=picTk.destroy)
    exit_btn.place(x=350,y=550)

    for_btn = Button(picTk,text='>>',command=lambda:forward(n),state=NORMAL)
    for_btn.place(x=650,y=550)
    picTk.mainloop()

def back(n):
    lb1.grid_forget()
    minus1 = photoimage_list[n-1]
    lb1.configure(image=minus1)
    lb1.image = minus1
    for_btn.configure(state=NORMAL,command=lambda:forward(n+1))
    back_btn.configure(state=NORMAL,command=lambda:back(n-1))
    lb1.place(x=10,y=10)
    back_btn.place(x=50,y=550)
    exit_btn.place(x=350,y=550)
    for_btn.place(x=650,y=550)
    if minus1 == photoimage_list[0]:
        back_btn.configure(state=DISABLED)

def forward(n):
    lb1.grid_forget()
    next2 = photoimage_list[n+1]
    lb1.configure(image=next2)
    lb1.image = next2
    for_btn.configure(state=NORMAL,command=lambda:forward(n+1))
    back_btn.configure(state=NORMAL,command=lambda:back(n-1))
    lb1.place(x=10,y=10)
    back_btn.place(x=50,y=550)
    exit_btn.place(x=350,y=550)
    for_btn.place(x=650,y=550)
    if next2 == photoimage_list[-1]:
        for_btn.configure(state=DISABLED)


tk = Tk()
tk.title('Home')

Label(tk,text='Photos of Dominic').pack(pady=10)
Button(tk,text='View Album',command=selection).pack(pady=10)


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "E:\Python\Python Projects Fun\photo_album.py", line 13, in selection
    im = Image.open(filepath)
AttributeError: type object 'Image' has no attribute 'open'


    
    

I changed to glob.glob('photos/*.gif') to indicate the files with gif format inside the folder 'photos' but however I still got error that the photos cannot be loaded on the new window. I would like to ask is there any missing in order to show the photo after I clicked the button? Is there problems on the line im = Image.open(filepath) that need to be changed? Because I already write pic = ImageTk.PhotoImage(image=im).

  • Your updated code removes `Image` from `from PIL import Image, ImageTk`. So you are using built-in `Image` class which does not have `open()` function. – acw1668 Aug 01 '20 at 03:18
  • but why when I clicked the next one then clicked the previous button the photo shown is not the same as before? – Dominic Sham Aug 01 '20 at 03:31
  • Try to print the value of `n` inside `back()` and `forward()` and you will see that the value of `n` passed to `back()` and `forward()` is not what you expect. – acw1668 Aug 01 '20 at 03:45
  • When I ```print(n-1)``` It returns to the value like 24 returns to 21 so if I want to return to 23 what should I change? – Dominic Sham Aug 01 '20 at 03:57
  • Change `command=lambda:forward(n+1)` to `command=lambda:forward(n-1)` inside `back()` function and `command=lambda:back(n-1)` to `command=lambda:back(n+1)` inside `forward()` function. – acw1668 Aug 01 '20 at 04:09
  • I changed ```n-1``` into ```n+1``` and it works – Dominic Sham Aug 01 '20 at 06:05
  • Please consider to up vote and accept the answer to your questions, like [the-height-and-width-cant-be-adjusted-when-i-change-the-size](https://stackoverflow.com/questions/63191422/the-height-and-width-cant-be-adjusted-when-i-change-the-size), [hangman-cannot-update-image-one-by-one](https://stackoverflow.com/questions/63168956/hangman-cannot-update-image-one-by-one) and etc. – acw1668 Aug 01 '20 at 08:07

0 Answers0