santhon88
santhon88

Reputation: 62

Display multiple images in Python Tkinter Sequentially

I want to make a program that displays an initial image, then shows the newest image in a folder.

I have everything working except the Tkinter panel displays the newest image below the first image in the window. I cannot figure out a way to destroy the original or to make another panel without the old image. I copied the relevant code below, not sure if it will execute error free. My .py file runs fine without any errors, just the unwanted results. Any help would be great.

from Tkinter import *
import Image, ImageTk
import datetime
import sys, os

FILE_PATH = "C:\\easy\\Pics"
#------------------------------------------------------------------------#
def quitX():
    root.destroy()
    sys.exit()

#------------------------------------------------------------------------#
def prg_task():
    # code section removed....

    # continue code

    im = Image.open("C:\easy\imageNEW.jpg")

    image1 = ImageTk.PhotoImage(im)

    # get the image size
    w = image1.width()
    h = image1.height()+10

    # position coordinates of root 'upper left corner'
    x = 500
    y = 200

    # make the root window the size of the image
    root.geometry("%dx%d+%d+%d" % (w, h, x, y))

    # root has no image argument, so use a label as a panel
    panel1 = Label(root, image=image1)
    panel1.pack(side='top', fill='both')

    # put a button on the image panel to test it
    button2 = Button(panel1, text='Close Window', command=quitX)
    button2.pack(side='bottom')

    # save the panel's image from 'garbage collection'
    panel1.image = image1

    root.after(10000, prg_task)

##########################################################################
# START CODE EXECUTION
# Initializing non-constant global variables

root = Tk()
root.title("Camera")
initIMG = Image.open("C:\easy\No-Image-Available.jpg")
tkimage = ImageTk.PhotoImage(initIMG)
panel1 = Label(root,image=tkimage)
panel1.pack(side='top', fill='both')
#------------------------------------------------------------------------#

# After 0.1s, attempt to grab still image from IP Camera
root.after(100, prg_task)
root.mainloop()

#------------------------------------------------------------------------#

Upvotes: 1

Views: 4193

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

If you are only going to show one image at a time you should create the panel and label only once. Each time you want to show a new image, create the image and then do somethign like self.panel1.configure(image=new_image).

Upvotes: 1

Related Questions