Programme Pendu Python-Tkinter

Résolu
alebrl Messages postés 3 Statut Membre -  
 vamosalaplaya -
Bonjour,

Dans le cadre de l'option ISN nous devons réaliser un projet qui sera pour nous un pendu.

Tout fonctionne bien cependant l'image ne change pas lorsque le nombre d'échec augmente. J'ai beau chercher je ne vois pas où est le problème ni comment faire.

Merci de votre attention et pour votre éventuelle aide.

#!/usr/bin/python

# -*- coding: latin-1 -*-

 # ----------------------------------------------------- Programme principal------------------------------------------------------------

#On importe les modules

from random import *

import Tkinter as tk

import time

# Listes de mots 

liste_mots_sport = ['TENNIS', 'BADMINTON','BASKET-BALL','FOOTBALL', 'TENNIS DE TABLE','RUGBY','WATERPOLO','EQUITATION','DANSE','HOCKEY']

liste_mots_histoire = ['NAPOLEON','LUCY', 'WATERLOO', 'CHARLEMAGNE', 'ABRAHAM LINCOLN', 'NELSON MANDELA', 'SIMONE VEIL', 'NEWTON', 'DARWIN','GALILEE']

liste_mots_prenoms = ['SALOME', 'ALEXANDRA', 'AGATHE', 'MARIANNE', 'MYRIAM', 'AURELIA', 'VINCENT', 'STANISLAS', 'MARCELLE', 'MONIQUE']

liste_mots_animaux = [ 'CHIEN', 'CHAT', 'SOURIS', 'LAPIN', 'CASTOR', 'SINGE', 'AIGLE'] 

liste_mots_pays_villes = ['FRANCE', 'LAVAL', 'BRESIL', 'AUSTRALIE', 'ETATS-UNIS', 'SAO-PAULO', 'ITALIE', 'VENISE', 'TUNISIE', 'ALLEMAGNE']

liste_mots_fruits_legumes = ['BANANE', 'FRAISE', 'POIRE','MANGUE','HARICOT','TOMATE', 'POMME', 'LITCHI', 'CERISE', 'FRAMBOISE']

# Choix du thème et du mot aléatoire

motmystere= ""

def Sport() :

global motmystere, word, echecs

motmystere = choice(liste_mots_sport)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im0.gif')

def Histoire() :

global motmystere, word, echecs

motmystere = choice(liste_mots_histoire)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im0.gif')

def Prenoms() :

global motmystere, word, echecs

motmystere = choice(liste_mots_prenoms)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im0.gif')

def Animaux() :

global motmystere, word, echecs

motmystere = choice(liste_mots_animaux)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im0.gif')

def  Pays_et_villes() :

global motmystere, word, echecs

motmystere = choice(liste_mots_pays_villes)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im8.gif')

def Fruits_et_legumes() :

global motmystere, word, echecs

motmystere = choice(liste_mots_fruits_legumes)

print(motmystere)

echecs = 0

word = "*" * len(motmystere)

afficher_mot(word)

image = tk.PhotoImage(file='im0.gif')

def Aleatoire() :

global motmystere, word, echecs

liste_aleatoire = randint(1, 6)

if liste_aleatoire == 1:

                Sport()

if liste_aleatoire == 2:

Histoire()

if liste_aleatoire == 3: 

Prenoms()

if liste_aleatoire == 4:

Animaux()

if liste_aleatoire == 5:

Pays_et_villes()

else:

Fruits_et_legumes()

def Recommencer() :

word = ""

afficher_mot(word)

        #image = tk.PhotoImage(file='im0.gif')

        #ord = "Choisissez un thème"

#def remove_accents(s):

    #alpha1 = "àÀâÂäÄåÅçÇéÉèÈêÊëËîÎïÏôÔöÖùÙûÛüÜÿY"

    #alpha2 = "aAaAaAaAcCeEeEeEeEiIiIoOoOuUuUuUyY"

    #return s.translate(str.maketrans(alpha1, alpha2))

def lettre_entree():

global word, motmystere, echecs

lettre = zone_lettre_entree.get()

lettre = lettre.upper()

#str.decode(lettre)

print(lettre)

#zone_lettre_entree.set('')

        l = 0 # l represente un curseur se déplaçant de lettre en lettre

        lettre_dans_mot= False

print("dddd"+ motmystere)

#dessin = tk.PhotoImage(file="im8.gif")

        while l < len(motmystere): # On verifie pour chaque lettre du mot mystere si elle est egale a la lettre rentree      

                        if lettre == motmystere[l]: # Si la lettre rentrée correspond à la lième lettre du mot à trouver

                            word = word[:l] + lettre + word[l+1:]

                            l += 1

                            lettre_dans_mot = True

                            continue  #Retourne au While

                        else:

                            l += 1 # Si la lettre rentrée ne correspond pas à la lième lettre du mot à trouver on vérifie la lettre suivante

                            continue # Retourne au While

print(word)

        afficher_mot(word)

        if word == motmystere :# Le mot a ete trouve

                afficher_mot(word) #voir comment faire car c'etait dans le cahier des charges

                dessin = tk.PhotoImage(file="imgagne.gif")

                score += 1

if not lettre_dans_mot : # Si arrive a la fin de la verification du mot et la lettre n'en fait pas parti

                echecs += 1

print(echecs)

                nomImage = "im"+str(echecs)+".gif"

print(nomImage)

                dessin = tk.PhotoImage(file=nomImage)

zone_decor.create_image(1,1,anchor='nw', image=dessin)

zone_decor.grid(row=5, column=60, rowspan=12, columnspan=9)

                if echecs == 9 : #Trop d'erreurs

                        afficher_mot(motmystere)

                        dessin = tk.PhotoImage(file="imperdu.gif")

                        score -= 1 

def afficher_mot(mot): 

affichage_mot = tk.Label (fenetre, text="                                 ").grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10) # Efface le texte précédent

affichage_mot = tk.Label (fenetre, text=mot).grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10)

# ----------------------------------------------- Construction de l'interface graphique -----------------------------------------------

# On crée une fenêtre principale, racine de notre interface

fenetre = tk.Tk()

fenetre.title('Le Pendu')

#boutons

tk.Label(fenetre, text="Choisir un thème").grid(row=4, column=1, columnspan=2, padx=5, pady=2)

tk.Button(fenetre, text='Sport', relief='groove', command=Sport).grid(row=5, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)

tk.Button(fenetre, text='Histoire', relief='groove', command=Histoire).grid(row=6, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)

tk.Button(fenetre, text='Prénoms', relief='groove',command=Prenoms).grid(row=7, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)

tk.Button(fenetre, text='Animaux', relief='groove', command=Animaux).grid(row=8, column=1, columnspan=2,padx=5, pady=2,ipadx=6, ipady=6)

tk.Button(fenetre, text='Pays et villes', relief='groove', command=Pays_et_villes).grid(row=9, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)

tk.Button(fenetre, text='Fruits et légumes', relief='groove', command=Fruits_et_legumes).grid(row=10, column=1, columnspan=6,padx=5, pady=2, ipadx=6, ipady=6)

tk.Button(fenetre, text='Aléatoire', relief='groove', command=Aleatoire).grid(row=11, column=1, columnspan=6, padx=5, pady=2, ipadx=6, ipady=6)

#tk.Button(fenetre, text='Valider', relief='groove',).grid(row=15, column=15)

tk.Button(fenetre, text='Recommencer', relief='groove', command=Recommencer).grid(row=25, column=60, padx=3, pady=2)

tk.Button(fenetre, text='Quitter', relief='groove', command=fenetre.quit).grid(row=25, column=61)

tk.Label(fenetre, text="Score :").grid(row=18, column=60, padx=5, pady=2, ipadx=6, ipady=6)

tk.Label(fenetre, text='JEU DU PENDU').grid(row=1, column=40, columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)

tk.Label(fenetre, text="Entrer une lettre majuscule").grid(row=4, column=30,columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)

zone_lettre_entree = tk.Entry(fenetre, width=8, justify='left')

zone_lettre_entree.grid(row=5, column=30, columnspan=6, padx=5, pady=2, ipadx=6, ipady=6)

#id = C.create_image(15,15).grid(row=2, column=1, columnspan=15, padx=5, pady=2, ipadx=6, ipady=6) #image deco à mettre sous le titre pour que ca ne colle pas au titre

#id = C.create_image(15,15).grid(row=2, column=1, columnspan=50, padx=5, pady=2, ipadx=6, ipady=6)

#Espace mot à deviner

tk.Label (fenetre, text="Mot à deviner :").grid(row=9, column=30, columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)

affichage_mot = tk.Label (fenetre, text="____________").grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10)

tk.Button (fenetre, text="Valider", relief = 'groove', command=lettre_entree).grid(row=7, column=30, columnspan=6,padx=5, pady=2, ipadx=6, ipady=6)

zone_decor = tk.Canvas (fenetre, width=500, height=500)

dessin = tk.PhotoImage(file="im0.gif")

zone_decor.create_image(1,1,anchor='nw', image=dessin)

zone_decor.grid(row=5, column=60, rowspan=12, columnspan=9)

fenetre.mainloop()

fenetre.destroy()

2 réponses

  1. alebrl Messages postés 3 Statut Membre 4
     
    Ah oui, désolée
    #!/usr/bin/python
    # -*- coding: latin-1 -*-
    
     # ----------------------------------------------------- Programme principal------------------------------------------------------------
    
    #On importe les modules
    from random import *
    import Tkinter as tk
    import time
    
    # Listes de mots 
    liste_mots_sport = ['TENNIS', 'BADMINTON','BASKET-BALL','FOOTBALL', 'TENNIS DE TABLE','RUGBY','WATERPOLO','EQUITATION','DANSE','HOCKEY']
    liste_mots_histoire = ['NAPOLEON','LUCY', 'WATERLOO', 'CHARLEMAGNE', 'ABRAHAM LINCOLN', 'NELSON MANDELA', 'SIMONE VEIL', 'NEWTON', 'DARWIN','GALILEE']
    liste_mots_prenoms = ['SALOME', 'ALEXANDRA', 'AGATHE', 'MARIANNE', 'MYRIAM', 'AURELIA', 'VINCENT', 'STANISLAS', 'MARCELLE', 'MONIQUE']
    liste_mots_animaux = [ 'CHIEN', 'CHAT', 'SOURIS', 'LAPIN', 'CASTOR', 'SINGE', 'AIGLE'] 
    liste_mots_pays_villes = ['FRANCE', 'LAVAL', 'BRESIL', 'AUSTRALIE', 'ETATS-UNIS', 'SAO-PAULO', 'ITALIE', 'VENISE', 'TUNISIE', 'ALLEMAGNE']
    liste_mots_fruits_legumes = ['BANANE', 'FRAISE', 'POIRE','MANGUE','HARICOT','TOMATE', 'POMME', 'LITCHI', 'CERISE', 'FRAMBOISE']
    
    # Choix du thème et du mot aléatoire
    motmystere= ""
    score = 0
    
    def Sport() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_sport)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im0.gif')
    
    def Histoire() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_histoire)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im0.gif')
    
    def Prenoms() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_prenoms)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im0.gif')
    
    def Animaux() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_animaux)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im0.gif')	
    
    def  Pays_et_villes() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_pays_villes)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im8.gif')	
    
    def Fruits_et_legumes() :
    	global motmystere, word, echecs
    	motmystere = choice(liste_mots_fruits_legumes)
    	print(motmystere)
    	echecs = 0
    	word = "*" * len(motmystere)
    	afficher_mot(word)
    	image = tk.PhotoImage(file='im0.gif')
    
    
    def Aleatoire() :
    	global motmystere, word, echecs
    	liste_aleatoire = randint(1, 6)
    	if liste_aleatoire == 1:
                    Sport()
    	if liste_aleatoire == 2:
    		Histoire()
    	if liste_aleatoire == 3: 
    		Prenoms()
    	if liste_aleatoire == 4:
    		Animaux()
    	if liste_aleatoire == 5:
    		Pays_et_villes()
    	else:
    		Fruits_et_legumes()
    
    
    def Recommencer() :
    	word = ""
    	afficher_mot(word)
            image = tk.PhotoImage(file='im0.gif')
            echecs = 0
    	word = "______"
    
    
    #def remove_accents(s):
        #alpha1 = "àÀâÂäÄåÅçÇéÉèÈêÊëËîÎïÏôÔöÖùÙûÛüÜÿY"
        #alpha2 = "aAaAaAaAcCeEeEeEeEiIiIoOoOuUuUuUyY"
        #return s.translate(str.maketrans(alpha1, alpha2))
    
    
    def lettre_entree():
    	global word, motmystere, echecs, score
    	lettre = zone_lettre_entree.get()
    	lettre = lettre.upper()
    	print(lettre)
            l = 0 # l represente un curseur se déplaçant de lettre en lettre
            lettre_dans_mot= False
    	print("dddd"+ motmystere)
    	#dessin = tk.PhotoImage(file="im8.gif")
            while l < len(motmystere): # On verifie pour chaque lettre du mot mystere si elle est egale a la lettre rentree      
                            if lettre == motmystere[l]: # Si la lettre rentrée correspond à la lième lettre du mot à trouver
                                word = word[:l] + lettre + word[l+1:]
                                l += 1
                                lettre_dans_mot = True
                                continue  #Retourne au While
                            else:
                                l += 1 # Si la lettre rentrée ne correspond pas à la lième lettre du mot à trouver on vérifie la lettre suivante
                                continue # Retourne au While
    	print(word)
            afficher_mot(word)
            if word == motmystere :# Le mot a ete trouve
                    afficher_mot(word)
                    dessin = tk.PhotoImage(file="imgagne.gif")
                    score = "rosti"
    		print(score)
    		afficher_score(score)
    	if not lettre_dans_mot : # Si arrive a la fin de la verification du mot et la lettre n'en fait pas parti
                    echecs += 1
                    nomImage = "im"+str(echecs)+".gif"
    		print(nomImage)
                    dessin = tk.PhotoImage(file=nomImage)
    		zone_decor.create_image(1,1,anchor='nw', image=dessin)
    		zone_decor.grid(row=5, column=60, rowspan=12, columnspan=9)
                    if echecs == 9 : #Trop d'erreurs
                            afficher_mot(motmystere)
                            dessin = tk.PhotoImage(file="imperdu.gif")
                            score = "patate" 
    			print(score)
    			afficher_score(score)
    
    def afficher_mot(mot):
    	affichage_mot = tk.Label (fenetre, text="                              ").grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10)
    	affichage_mot = tk.Label (fenetre, text=mot).grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10)
    
    def afficher_score(points) :
    	affichage_score = tk.Label(fenetre, text="zfoidzjfkvfdejnkfev,nvkfe,jvfek").grid(row=18, column=60, padx=5, pady=2, ipadx=6, ipady=6)
    	#affichage_score = tk.Label(fenetre, text=points).grid(row=18, column=60, padx=5, pady=2, ipadx=6, ipady=6)
    
    # ----------------------------------------------- Construction de l'interface graphique -----------------------------------------------
    
    
    # On crée une fenêtre principale, racine de notre interface
    
    
    fenetre = tk.Tk()
    fenetre.title('Le Pendu')
    
    
    #boutons
    tk.Label(fenetre, text="Choisir un thème").grid(row=4, column=1, columnspan=2, padx=5, pady=2)
    tk.Button(fenetre, text='Sport', relief='groove', command=Sport).grid(row=5, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button(fenetre, text='Histoire', relief='groove', command=Histoire).grid(row=6, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button(fenetre, text='Prénoms', relief='groove',command=Prenoms).grid(row=7, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button(fenetre, text='Animaux', relief='groove', command=Animaux).grid(row=8, column=1, columnspan=2,padx=5, pady=2,ipadx=6, ipady=6)
    tk.Button(fenetre, text='Pays et villes', relief='groove', command=Pays_et_villes).grid(row=9, column=1, columnspan=2,padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button(fenetre, text='Fruits et légumes', relief='groove', command=Fruits_et_legumes).grid(row=10, column=1, columnspan=6,padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button(fenetre, text='Aléatoire', relief='groove', command=Aleatoire).grid(row=11, column=1, columnspan=6, padx=5, pady=2, ipadx=6, ipady=6)
    tk.Button (fenetre, text="Valider", relief = 'groove', command=lettre_entree).grid(row=7, column=30, columnspan=6,padx=5, pady=2, ipadx=6, ipady=6)
    
    
    tk.Button(fenetre, text='Recommencer', relief='groove', command=Recommencer).grid(row=25, column=60, padx=3, pady=2)
    tk.Button(fenetre, text='Quitter', relief='groove', command=fenetre.quit).grid(row=25, column=61)
    affichage_score = tk.Label(fenetre, text="Score :").grid(row=18, column=60, padx=5, pady=2, ipadx=6, ipady=6)
    
    tk.Label(fenetre, text='JEU DU PENDU').grid(row=1, column=40, columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)
    tk.Label(fenetre, text="Entrer une lettre majuscule").grid(row=4, column=30,columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)
    zone_lettre_entree = tk.Entry(fenetre, width=8, justify='left')
    zone_lettre_entree.grid(row=5, column=30, columnspan=6, padx=5, pady=2, ipadx=6, ipady=6)
    #id = C.create_image(15,15).grid(row=2, column=1, columnspan=15, padx=5, pady=2, ipadx=6, ipady=6) #image deco à mettre sous le titre pour que ca ne colle pas au titre
    #id = C.create_image(15,15).grid(row=2, column=1, columnspan=50, padx=5, pady=2, ipadx=6, ipady=6)
    
    #Espace mot à deviner
    tk.Label (fenetre, text="Mot à deviner :").grid(row=9, column=30, columnspan=6, padx=5, pady=2, ipadx=10, ipady=10)
    affichage_mot = tk.Label (fenetre, text="______").grid(row=10, column=30, columnspan=6, padx=5, pady=5, ipadx=10, ipady=10)
    
    
    
    
    zone_decor = tk.Canvas (fenetre, width=500, height=500)
    dessin = tk.PhotoImage(file="im0.gif")
    zone_decor.create_image(1,1,anchor='nw', image=dessin)
    zone_decor.grid(row=5, column=60, rowspan=12, columnspan=9)
    
    
    fenetre.mainloop()
    fenetre.destroy()
    5
    1. Utilisateur anonyme
       
      Utilise itemconfig(fenetre, image = dessin) à la place de create_image([...]). Ta fonction Pays_et_villages a une erreur je crois dans l'image (im8 à la place de im0)
      0
      1. alebrl Messages postés 3 Statut Membre 4 > Utilisateur anonyme
         
        Merci de votre aide. J'ai réussi à finir mon programme
        Bonne journée
        0
      2. vamosalaplaya > alebrl Messages postés 3 Statut Membre
         

        pourrais tu renvoyer ton code fini s'il te plait 

        0