Images sur Tkinter

Fermé
Rhydan1 Messages postés 382 Date d'inscription jeudi 3 juillet 2014 Statut Membre Dernière intervention 2 avril 2017 - 11 déc. 2016 à 16:44
_Ritchi_ Messages postés 21210 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 20 avril 2024 - 12 déc. 2016 à 19:47
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

_Ritchi_ Messages postés 21210 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 20 avril 2024 6 058
Modifié par _Ritchi_ le 12/12/2016 à 19:51
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