Displaying an image stored in a function

Viewed 57

I am working on a Python program that lets the user discover New-Caledonia's works of art (it's a smallish French island in the Pacific Ocean :) ).

I try to call a function that stores an image. But after hours of research I found out this webpage describing the problem which I have. What happens is that Python garbages my image because I have to "keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute." I tried to do it, but as I am a beginner, I don't really know how to do that.

Here is my script:

from tkinter import *

# Création de la fenêtre de menu
fenetre=Tk()
fenetre.title("ART'CAL")
Canevas=Canvas(fenetre, height=768,width=1346)
Canevas.pack()
# Insertion de l'image du menu
PhotoMenu = PhotoImage(file ='Diapositive1.gif')
Fond = Canevas.create_image(680, 350, image=PhotoMenu)

# Création de la fenêtre de règles du jeu
def Rules():
    fenetre=Tk()
    fenetre.title("Règles du jeu")
    CanevasRules=Canvas(fenetre, height=768,width=1346)
    CanevasRules.pack()
# Insertion de l'image des règles du jeu
    PhotoRules = PhotoImage(file ='Diapositive2.gif')
    Fond = CanevasRules.create_image(680, 350, anchor=CENTER, image=PhotoRules)
    Suivant = Button(CanevasRules, text="Suivant")
    Suivant.place(x=600, y=510)
    Suivant.config(font=('Product Sans',20,'bold'))

# Création et personnalisaton du bouton "Découvrir"
Decouvrir = Button(Canevas, text="Découvrir")
Decouvrir.place(x=600, y=510)
Decouvrir.config(font=('Product Sans',20,'bold'), command=Rules)

fenetre.mainloop()

Everything works fine until I press DECOUVRIR ("Discover" in French). It doesn't display any image. Can anyone help me and show me how to display this image?

1 Answers

Two issues need modification: First, you shouldn't open new Tkinter root (Tk()), in order to open a new window use Toplevel. Second, in order to "save a copy of the image" just assign it as an attribute of your new window.

I hope this modified code helps you:

from tkinter import *

# Creation de la fenetre de menu
root=Tk()
root.title("ART'CAL")
Canevas=Canvas(root, height=768, width=1346)
Canevas.pack()
# Insertion de l'image du menu
PhotoMenu = PhotoImage(file ='Diapositive1.gif')
Fond = Canevas.create_image(680, 350, image=PhotoMenu)

# Creation de la fenetre de regles du jeu
def Rules():
    fenetre=Toplevel(root)  # open a new window using Toplevel
    fenetre.title("Regles du jeu")
    CanevasRules=Canvas(fenetre, height=768,width=1346)
    CanevasRules.pack()
    # Insertion de l'image des regles du jeu
    PhotoRules = PhotoImage(file ='Diapositive2.gif')
    Fond = CanevasRules.create_image(680, 350, anchor=CENTER, image=PhotoRules)
    Suivant = Button(CanevasRules, text="Suivant")
    Suivant.place(x=600, y=510)
    Suivant.config(font=('Product Sans',20,'bold'))
    fenetre.my_image = PhotoRules  # saving a copy of the image

# Creation et personnalisaton du bouton "Decouvrir"
Decouvrir = Button(Canevas, text="Decouvrir")
Decouvrir.place(x=600, y=510)
Decouvrir.config(font=('Product Sans',20,'bold'), command=Rules)

root.mainloop()
Related