Images sur Tkinter

Rhydan1 Messages postés 438 Statut Membre -  
_Ritchi_ Messages postés 21130 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour bonjour :)

Pour un exercice, je dois afficher une image dans un Canvas à l'aide de la librairie Tkinter.

Cependant lorsque je tente d'afficher l'image, j'ai ce message d'erreur :

_tkinter.TclError: unknown option "pyimage1"


Voici mon code :

from tkinter import *
 
 
 
def nouvelleQuestion():
    zone_image.create_image(0, 0, image)
 
 
fenetre = Tk()
 
image = PhotoImage(file="chat.gif")
 
zone_image = Canvas(fenetre, width=400, height=400, bg="white")
bouton_question = Button(fenetre, text="Nouvelle question", command=nouvelleQuestion)
bouton_sol1 = Button(fenetre, text="Chat", width=5)
bouton_sol2 = Button(fenetre, text="Chien", width=5)
bouton_sol3 = Button(fenetre, text="Âne", width=5)
 
 
zone_image.pack(side=LEFT)
bouton_question.pack()
bouton_sol1.pack()
bouton_sol2.pack()
bouton_sol3.pack()
fenetre.mainloop()


Cette erreur provient-elle de mon code, de python, ou de Tkinter ?

Merci pour votre aide :)

Cordialement.

1 réponse

  1. _Ritchi_ Messages postés 21130 Date d'inscription   Statut Contributeur Dernière intervention   6 135
     
    Bonjour,

    Voici le code modifié:
    from mtTkinter import *
    
    def nouvelleQuestion():
        zone_image.create_image(0, 0, image=image)
    
    
    fenetre = Tk()
    
    image = PhotoImage(file="chat.gif")
    
    zone_image = Canvas(fenetre, width=400, height=400, bg="white")
    bouton_question = Button(fenetre, text="Nouvelle question", command=nouvelleQuestion())
    bouton_sol1 = Button(fenetre, text="Chat", width=5)
    bouton_sol2 = Button(fenetre, text="Chien", width=5)
    bouton_sol3 = Button(fenetre, text="Ane", width=5)
    
    
    zone_image.pack(side=LEFT)
    bouton_question.pack()
    bouton_sol1.pack()
    bouton_sol2.pack()
    bouton_sol3.pack()
    fenetre.mainloop()
    


    Les modifications sont:
    - l'import de mtTkinter
    - dans def nouvelleQuestion(): il faut indiquer quelle image tu veux afficher d'où le image=image
    - dans la définition du bouton "Nouvelle question", il manquait les () dans "command=nouvelleQuestion()"

    Ritchi
    1