Hello ,
I am making tries to display images in different tkinter windows , in every window displaying a different image , for this i made this code :

# -*- coding: cp1252 -*-

from Tkinter import *
import Tkinter as tk
import ttk


from PIL import ImageTk, Image
import os

def interface2():
    fenetre2=Toplevel()
    fenetre2.geometry("500x400")
    fenetre2.title('                                     ')

    f1 = Frame(fenetre2, bg="blue",  width=500, height=400)
    f1.pack( fill=X, expand=0)

    print modelidmodifvar2


    print "image%s.jpg"%(modelidmodifvar2)
    img2 = ImageTk.PhotoImage(Image.open("image%s.jpg"%modelidmodifvar2))

    labimg.config( image=img2 )

    print 'ok'    

    lab1 = Label(fenetre2, text="Gestion de ressources informatiques" , bg = "blue", fg = "white" )
    lab1.place ( x=152 , y=25 )

    buttonquit = Button(fenetre2, text = "Quitter", command = fenetre2.destroy)        
    buttonquit.place( x=224 , y=374 )

    #Display

    fenetre.withdraw()



fenetre=Tk()
fenetre.geometry("500x400")
fenetre.title('                                     ')

f1 = Frame(fenetre, bg="blue",  width=500, height=400)
f1.pack( fill=X, expand=0)

global modelidmodifvar
global modelidmodifvar2

global labimg

modelidmodifvar = StringVar
modelidmodifvar2 = StringVar
modelidmodifvar = '12013'
modelidmodifvar2 = '22013'

labimg = Label(fenetre)
labimg.place(x=10,y=10)

img = ImageTk.PhotoImage(Image.open("image%s.jpg"%modelidmodifvar))


labimg.config( image=img )

lab1 = Label(fenetre, text="Gestion de ressources informatiques" , bg = "blue", fg = "white" )
lab1.place ( x=152 , y=25 )


butval = Button(fenetre, text = "valider", command = interface2)        
butval.place( x=224 , y=54 )

buttonquit = Button(fenetre, text = "Quitter", command = fenetre.destroy)        
buttonquit.place( x=224 , y=374 )

#Display

fenetre.mainloop()

In the first window , it dispays well the first image :"image12013.jpg" , but on the second window , that display nothing , and don't show any error message !
Can i know why plz ?

Generally, create all image objects in main to be persistent.
If you create the image object in a function it will tie it to a
function scope variable, Then when the function is done the image
will be garbage collected.

One potential way around this, try it in your code ...

labimg.config( image=img2 )
# save label image from garbage collection!
labimg.image = img2

Edited 3 Years Ago by vegaseat

Hello ,
Thank you for your answer vegaseat , i tried both labimg.config(image=img2) and labimg.image = img2 but it shows a window without image , and with out error .
Today , i tried to make "fenetre2" as main window , and destroying the first , and create a new label , and it works . But i i'm trting for this simple example to apply it for a small programm that connect with database ( where the window is a Toplevel() one ) .

This question has already been answered. Start a new discussion instead.