Python Project on Image Viewer
This project is developed in Python using Tkinter library. UI two buttons - Prev & Next to move images . Users are allowed to modify the code as per their requirments. This project is developed on Python 3.8.3. Please note this is very basic project.
from tkinter import *
from PIL import ImageTk, Image
import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
logging.debug('A debug message!')
root = Tk()
root.title('How to display images in Python')
root.geometry("620x500") #You want the size of the app to be 620x500
root.resizable(0, 0) #Don't allow resizing in the x or y direction
root.iconbitmap('c:/favicon.ico')
buttonExit = Button(root, text="Exit Program", command=root.quit)
img1 = ImageTk.PhotoImage(Image.open("c:/img/1.jpg"))
img2 = ImageTk.PhotoImage(Image.open("c:/img/2.jpg"))
img3 = ImageTk.PhotoImage(Image.open("c:/img/3.jpg"))
img4 = ImageTk.PhotoImage(Image.open("c:/img/4.jpg"))
img5 = ImageTk.PhotoImage(Image.open("c:/img/5.jpg"))
img6 = ImageTk.PhotoImage(Image.open("c:/img/6.jpg"))
img7 = ImageTk.PhotoImage(Image.open("c:/img/7.jpg"))
img_list = [img1, img2, img3, img4, img5, img6, img7]
global imgNumber
imgNumber = 0
label = Label(image=img1, padx=0, pady=0, height=400, width=600)
def Prev():
global label
global buttonPrev
global buttonNext
global imgNumber
imgNumber = imgNumber - 1
logging.debug('in Prev() imgNumber %d', imgNumber)
label.grid_forget()
logging.debug('img is %s', img_list[imgNumber])
label = Label(image=img_list[imgNumber], height=400, width=600)
label.grid(row=0, column=0, columnspan=3)
buttonNext = Button(root, text=">>", command=lambda: Next())
buttonPrev = Button(root, text="<<", command=lambda: Prev())
if imgNumber == 0:
buttonPrev = Button(root, text="<<", state=DISABLED)
buttonPrev.grid(row=1, column=0)
else:
buttonNext = Button(root, text=">>", state=NORMAL, command=lambda: Next())
buttonNext.grid(row=1, column=2)
def Next():
global label
global buttonPrev
global buttonNext
global imgNumber
imgNumber = imgNumber + 1
logging.debug('in Next() imgNumber %d', imgNumber)
label.grid_forget()
logging.debug('img2 is %s', img_list[imgNumber])
label = Label(image=img_list[imgNumber], height=400, width=600)
label.grid(row=0, column=0, columnspan=3)
buttonNext = Button(root, text=">>", command=lambda: Next())
#buttonPrev = Button(root, text="<<", command=lambda: Prev())
if imgNumber == 6:
buttonNext = Button(root, text=">>", state=DISABLED)
buttonNext.grid(row=1, column=2)
else:
buttonPrev = Button(root, text="<<", state=NORMAL, command=lambda: Prev())
buttonPrev.grid(row=1, column=0)
buttonPrev = Button(root, text="<<", padx=10, pady=20, state=DISABLED)
buttonNext = Button(root, text=">>", padx=10, pady=20, command=lambda: Next())
label.grid(row=0, column=0, columnspan=3)
buttonPrev.grid(row=1, column=0)
buttonExit.grid(row=1, column=1)
buttonNext.grid(row=1, column=2)
root.mainloop()