Bonjour, je dois faire un programme dans le cadre de la matière d'ISN... Celui-ci est le dernier, en deux fichier .py :
1 - v3.0.py
from tkinter import * #Nous utilisons Tkinter
from time import *
from random import *
from pendu1234 import *
fen=Tk()
c=Canvas(fen,cursor='pencil',width=800,height=600) #Dimensions Page+Curseur
fen.title('Blop & Cie')
c.pack()
def Clic(event):
Interagir avec notre personnage
X = event.x
Y = event.y
if 50<X<240 and 350<Y<550 :
c.itemconfigure(perso,image=perso2)
fen.update()
sleep(3)
c.itemconfigure(perso,image=perso1)
fen.update()
Aller dans le Tunnel
if 430<X<470 and 390<Y<420 :
c.itemconfigure(fond,image=fond2)
fen.update()
retour fond de base
if 300<X<500 and 0<Y<100 :
c.itemconfigure(fond,image=fond1)
fen.update()
Pendu
if 480<X<550 and 240<Y<300 :
pendu()
fond1 = PhotoImage(file="fond.gif")
fond2 = PhotoImage(file="fond2.gif")
perso1 = PhotoImage(file="perso1.gif") #On créé notre personnage
perso2 = PhotoImage(file="perso1.1.gif")
fond = c.create_image(400,300, image=fond1)
perso = c.create_image(150,450, image=perso1)
c.bind('<Button-1>', Clic) #clic gauche de la souris
fen.mainloop() #refresh
2 - python1234.py
from tkinter import *
from time import *
from random import choice
liste_mots =["JAMBON","MAISON","ECOLE","TRAVAIL","LOISIR","CHAT","TABLE","MUSIQUE","POISSON","CHIEN","BALLON"]
def lettre_dans_mot(lettre) :
global partie_en_cours, mot_partiel, mot_choisi, nb_echecs, image_pendu
if partie_en_cours :
nouveau_mot_partiel = ""
lettre_dans_mot = False
i=0
while i<len(mot_choisi):
if mot_choisi[i]==lettre:
nouveau_mot_partiel = nouveau_mot_partiel + lettre
lettre_dans_mot = True
else:
nouveau_mot_partiel = nouveau_mot_partiel + mot_partiel[i]
i+=1
mot_partiel = nouveau_mot_partiel
afficher_mot(mot_partiel) #vérifie si la lettre est bien dans le mot
if not lettre_dans_mot : # lettre fausse. Changer le dessin
nb_echecs += 1
nomFichier = "pendu_"+str(nb_echecs)+".gif"
photo=PhotoImage(file=nomFichier)
image_pendu.config(image=photo)
image_pendu.image=photo
if nb_echecs == 8: # trop d'erreurs. terminé
partie_en_cours = False
afficher_mot(mot_choisi)
fenetre = Tk()
bouton2 = Button(fenetre,text='Quitter',command=fenetre.quit)
bouton2.pack(side=RIGHT)
bouton1 = Button(fenetre,text='Recommencer',command=init_jeu)
bouton1.pack(side=RIGHT)
elif mot_partiel == mot_choisi: # le mot a été trouvé
partie_en_cours = False
def afficher_mot(mot):
global lettres
mot_large = ""
i=0
while i<len(mot): # ajoute un espace entre les lettres
mot_large = mot_large + mot[i] + " "
i+=1
canevas.delete(lettres)
lettres = canevas.create_text(320,60,text=mot_large,fill='black',font='Courrier 30')
def init_jeu():
global mot_choisi, mot_partiel, image_pendu, lettres, nb_echecs
global partie_en_cours, liste_mots
nb_echecs = 0
partie_en_cours = True
mot_choisi = choice(liste_mots).rstrip()
mot_choisi = mot_choisi.upper()
mot_partiel = "-" * len(mot_choisi)
canevas.delete(lettres)
afficher_mot(mot_partiel)
photo=PhotoImage(file="pendu_0.gif")
image_pendu.config(image=photo)
image_pendu.image=photo
def pendu () :
global fenetre,canevas,bouton,photo,image_pendu,lettres
fenetre = Tk()
fenetre.title("Le jeu du pendu")
canevas = Canvas(fenetre, bg='white', height=500, width=620)
canevas.pack(side=BOTTOM)
bouton = [0]*26
for i in range(26):
bouton[i] = Button(fenetre,text=chr(i+65),command=lambda x=i+65:lettre_dans_mot(chr(x)))
bouton[i].pack(side=LEFT)
photo=PhotoImage(file="pendu_0.gif")
image_pendu = Label(canevas, image=photo, border=0)
image_pendu.place(x=140, y=120)
lettres = canevas.create_text(320,60,text="",fill='black',font='Courrier 30')
init_jeu()
fenetre.mainloop()
fenetre.destroy()
Les messages d'erreur sont les suivants :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "E:\ISN projet\v3.0.py", line 32, in Clic
pendu()
File "E:\ISN projet\pendu1234.py", line 76, in pendu
image_pendu = Label(canevas, image=photo, border=0)
File "C:\Python33\lib\tkinter\__init__.py", line 2596, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 2075, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage5" doesn't exist
Et lorsque je clique sur une lettre du pendu (en premier lieu, ça ne m'affiche pas d'image) :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "E:\ISN projet\pendu1234.py", line 72, in <lambda>
bouton[i] = Button(fenetre,text=chr(i+65),command=lambda x=i+65:lettre_dans_mot(chr(x)))
File "E:\ISN projet\pendu1234.py", line 7, in lettre_dans_mot
if partie_en_cours :
NameError: global name 'partie_en_cours' is not defined