Erreur que je ne comprends pas

Fermé
Benjamin - Modifié par mamiemando le 1/03/2017 à 10:37
mamiemando Messages postés 33540 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 21 février 2025 - 1 mars 2017 à 10:44
Bonjour,

J'ai pour projet de créer un pendu avec tk inter , donc avec une fenêtre graphique .
J'aimerais insérer une image et il met cet erreur :

Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "E:\ICN\pendu_fen\fenetre.2.2(à modifier).py", line 98, in <module>
etape1=PhotoImage(file="E:\\ICN\\etapeo1.gif")
File "C:\EduPython\App\lib\tkinter\__init__.py", line 3421, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\EduPython\App\lib\tkinter\__init__.py", line 3362, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image


Et mon code et

#code 
from tkinter import *
from random import *

def chaine (liste):
    c=''
    for i in range (0,len(liste)):
        c=c+'-'+liste[i]
    return c.upper()


#remplace le mot par des etoiles
def remplacer_mot_etoile (mot1,liste):
    mot_cache = ""
    for lettre in mot1:
        if lettre in liste :
            mot_cache += lettre
        else :
            mot_cache += "*"
    return mot_cache

def choisir_mot(liste):
    i=randrange(len(liste))
    return liste[i]

erreur=0
def recup_lettre(event):

global lettres_trouvees,lettre,mot_trouve,mot,E,zone_image
    lettre=saisir.get()
    if len(lettre)!=1 :
        text7.config(text='rentrer une SEULE lettre')
        saisir.delete(0,END)
        return lettre

saisir.delete(0,END)


print (lettre)


if lettre in lettres_trouvees: # La lettre a deja ete choisie
        text7.config(text='La lettre a deja ete choisie')

elif lettre in mot: # La lettre est dans le mot a trouver
        lettres_trouvees.append(lettre)
        text7.config(text='bien joue')

else:#ce n'est pas la bonne lettre
        lettres_trouvees.append(lettre)
        E+=1
        zone_image.create_image(300,300,image=etapes[E])
        print (E)
        text7.config(text='Ce n est pas une bonne lettre')
    mot_trouve=remplacer_mot_etoile(mot,lettres_trouvees)
    text2.config(text=mot_trouve)
    text6.config(text=chaine(lettres_trouvees))
    text9.config(text=lettre)
    #Ici , si on depasse 10 erreurs , alors on a perdu et le jeu est fini
    if erreur>=9:
        text7.config(text='Pendu , tu as perdu , tu es nul')
        saisir.destroy()
        text4.destroy()
        text9.destroy()
        text8.destroy()

if mot==mot_trouve:
        text7.config(text='Felicitation , vous avez trouve le mot '+ mot)
        saisir.destroy()
        text4.destroy()
        text9.destroy()
        text8.destroy()



global lettres_trouvees,mot,lettre,mot,E,zone_image

E=0 # numéro de l’étape

#la liste de mot:
liste_mot=[
    "voiture",
    "maison",
    "portable",
    "pierre",
    "casque",
    "ingenieur",
    "son",
    "patate",
    "anticonstitutiellement"]

etape1=PhotoImage(file="E:\\ICN\\etapeo1.gif")
etape2=PhotoImage(file="E:\\ICN\\etapeo2.gif")
etape3=PhotoImage(file="E:\\ICN\\etapeo3.gif")
etape4=PhotoImage(file="E:\\ICN\\etapeo4.gif")
etape5=PhotoImage(file="E:\\ICN\\etapeo5.gif")
etape6=PhotoImage(file="E:\\ICN\\etapeo6.gif")
etape7=PhotoImage(file="E:\\ICN\\etapeo7.gif")
etape8=PhotoImage(file="E:\\ICN\\etapeo8.gif")
etape9=PhotoImage(file="E:\\ICN\\etapeo9.gif")
etape10=PhotoImage(file="E:\\ICN\\etapeo10.gif")

etapes=[etape1,etape2,etape3,etape4,etape5,etape6,etape7,etape8,etape9,etape10]

lettres_trouvees=[]

mot=choisir_mot(liste_mot)
mot_trouve=remplacer_mot_etoile(mot,lettres_trouvees)


fenetre = Tk()
fenetre.title("Le jeu du pendu")

zone_image=Canvas(fenetre, bg='black',width=600,height=600,relief="groove")
zone_image.create_image(300,300,image=etapes[E])
zone_image.grid(column=2,columnspan=2,rowspan = 2)


saisir=StringVar()
saisir.set




text1=Label(fenetre,text='mot a  chercher')
text1.grid(row=0)

text2=Label(fenetre,text=mot_trouve)
text2.grid(row=0,column=1)

text4=Label(fenetre,text='lettre' )
text4.grid(row=1)

saisir= Entry(textvariable=saisir,width=7,justify=CENTER)
saisir.bind("<Return>", recup_lettre)
saisir.grid(row=1,column=1)



text5=Label(fenetre,text='Lettre Joue :')
text5.grid(row=2,column=2)

text6=Label(fenetre,text='')
text6.grid(row=2,column=3)


text7=Label(fenetre,text='')
text7.grid(row=3,columnspan=2)

text8=Label(fenetre,text='derniere lettre joue')
text8.grid(row=4,column=1)

text9=Label(fenetre,text='')
text9.grid(row=4,column=2)


menubar=Menu(fenetre)

menu1=Menu(menubar,tearoff=0)
menu1.add_command(label='Quitter',command=fenetre.destroy)
menu1.add_separator()
menubar.add_cascade(label='Option',menu=menu1)


fenetre.config(menu=menubar)
fenetre.mainloop()


1 réponse

mamiemando Messages postés 33540 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 21 février 2025 7 828
1 mars 2017 à 10:44
Bonjour,


Vérifie si quand tu arrives à la ligne qui déclenche ton erreur,
tkinter.Tk()
a bien été appelé.
https://python.developpez.com/faq/?page=Divers
https://stackoverflow.com/questions/10236857/python-tkinter-error-too-early-to-create-image

Autre conseil qui n'a rien à voir, évite les
from ... import *
, c'est une mauvaise habitude qui peut engendrer des collisions quand deux fonctions s'appellent pareil entre deux modules.

Ça te forcera notamment à écrire proprement les appels à PhotoImage et la création de Tk().

Dernier conseil, en programmation, les variables globales en programmations sont souvent une mauvaise idée car elles sont sources de problème, et donc à proscrire autant que possible.

Bonne chance
0