Creating simply image gallery in Python, Tkinter & PIL

python, python-imaging-library, tkinter, user-interface

Solution

Change image by setting image item: `Label['image'] = photoimage_obj`

import Image
import ImageTk
import Tkinter

image_list = ['1.jpg', '2.jpg', '5.jpg']
text_list = ['apple', 'bird', 'cat']
current = 0

def move(delta):
    global current, image_list
    if not (0 <= current + delta < len(image_list)):
        tkMessageBox.showinfo('End', 'No more image.')
        return
    current += delta
    image = Image.open(image_list[current])
    photo = ImageTk.PhotoImage(image)
    label['text'] = text_list[current]
    label['image'] = photo
    label.photo = photo


root = Tkinter.Tk()

label = Tkinter.Label(root, compound=Tkinter.TOP)
label.pack()

frame = Tkinter.Frame(root)
frame.pack()

Tkinter.Button(frame, text='Previous picture', command=lambda: move(-1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Next picture', command=lambda: move(+1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Quit', command=root.quit).pack(side=Tkinter.LEFT)

move(0)

root.mainloop()

Problem

So, I'm on simple project for a online course to make an image gallery using python. The thing is to create 3 buttons one Next, Previous and Quit. So far the quit button works and the next loads a new image but in a different window, I'm quite new to python and GUI-programming with Tkinter so this is a big part of the begineers course. So far my code looks like this and everything works. But I need help in HOW to make a previous and a next button, I've used the NEW statement so far but it opens in a different window. I simply want to display 1 image then click next image with some simple text. ``` import Image import ImageTk import Tkinter root = Tkinter.Tk(); text = Tkinter.Text(root, width=50, height=15); myImage = ImageTk.PhotoImage(file='nesta.png'); def new(): wind = Tkinter.Toplevel() wind.geometry('600x600') imageFile2 = Image.open("signori.png") image2 = ImageTk.PhotoImage(imageFile2) panel2 = Tkinter.Label(wind , image=image2) panel2.place(relx=0.0, rely=0.0) wind.mainloop() master = Tkinter.Tk() master.geometry('600x600') B = Tkinter.Button(master, text = 'Previous picture', command = new).pack() B = Tkinter.Button(master, text = 'Quit', command = quit).pack() B = Tkinter.Button(master, text = 'Next picture', command = new).pack() master.mainloop() ```

Original source