| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 
 | # -*- coding: utf8 -*-
 
from Tkinter import * 
import tkMessageBox
import random
 
from random import choice
 
trouver = lambda mot, lettre: [i for i, car in enumerate(mot) if car==lettre]
 
class Pendu(Frame): 
    def __init__(self, parent): 
        Frame.__init__(self, parent) 
        self.mot_claire = ""
        self.mot_cache  = ""
        self.nb_try = 0
 
        #self.Parent.title("Le jeu du pendu")
 
        bouton = Button(self, text='Quitter', command=self.destroy)
        bouton.grid(row=0 , column=0)
 
        bouton = Button(self, text='Jouer', command = self.init_game)
        bouton.grid(row=1 , column=0, sticky='E')
 
        bouton = Button(self, text='OK', command= self.play)
        bouton.grid(row=2 , column=0,sticky='E')
 
        bouton= Button(self, text='Instructions', command= self.regle)
        bouton.grid(row=3 , column=0,sticky='E')
 
        label = Label(self, text='Mot à trouver:')
        label.grid(row =0, column=1)
 
        label = Label(self, text='Entrez votre lettre:')
        label.grid(row =1, column=1)
 
        self.entry = Entry(self)
        self.entry.grid(row =0, column =2)
 
        self.user_entry = Entry(self)
        self.user_entry.grid(row =1, column =2)
 
    def regle():
        pass
 
    def try_letter(self, letter):
        solve = trouver(self.mot_claire, letter)
        if len(solve) > 0 :
            tab = list(self.mot_cache)
            for place in solve :
                tab[place] = letter
            self.mot_cache = "".join(tab)
 
    def refresh(self):
        self.entry.delete(0, END)
        self.entry.insert(0, self.mot_cache)
 
    def init_mot(self):
        liste=["riz","chameau"]
        self.mot_claire = choice(liste)
        self.mot_cache  = ''.join(['-' for lettre in self.mot_claire]) 
 
    def init_game(self):
        self.init_mot()
        self.refresh()
 
    def win_loss(self):
        if self.nb_try > 13 :
            tkMessageBox.showinfo(self, message = "Vous avez perdu !")
        else :
            if '-' not in self.mot_cache :
                tkMessageBox.showinfo(self, message = "Vous avez gagné !")
 
    def play(self):
        saisie = self.user_entry.get()
        self.user_entry.delete(0, END)
        if saisie not in self.mot_claire :
            self.nb_try += 1
            tkMessageBox.showinfo(self, message = "La lettre n'appartient pas au mot")
            return False
        else:
            self.try_letter(saisie)
            self.refresh()
        self.win_loss()
 
root = Tk() 
pendu = Pendu(root) 
pendu.pack() 
root.mainloop() | 
Partager