1 2 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
| from tkinter import *
import random
import tkinter.font as font
class Window(Tk):
def __init__(self):
super().__init__()
self.NB_MAX = 10
self.NB_MIN = 1
self.title('Trouve le nombre')
self.helv10 = font.Font(family='Helvetica', size=10, weight='bold')
Label(self, text='Ton chiffre entre 1 et 10 :', width=20,
height=10, font=self.helv10).grid(row=0, column=0)
self.reponse = Entry(self)
self.reponse.focus_set() # impose le focus
self.reponse.grid(row=0, column=1)
Button(self, text='Valider', command=self.test).grid(
row=1, columnspan=2, sticky='nsew')
Button(self, text='Rejouer', command=self.jouer).grid(row=4, column=0)
Button(self, text='Quitter', command=self.destroy).grid(row=4, column=1)
self.affichage = Label(self, width=30, height=10,
font=self.helv10) # affiche les réponses
self.affichage.grid(row=3, columnspan=2, sticky='nsew')
self.nombre = random.randrange(self.NB_MIN, self.NB_MAX+1)
def repondre(self, affiche):
self.affichage['text'] = affiche
self.reponse.delete(0, END)
def test(self):
if self.affichage["text"] != "GAGNE":
print('test reponse', self.reponse.get())
if int(self.reponse.get()) == self.nombre:
self.repondre('GAGNE')
self.reponse.delete(0, "end")
else:
self.affichage["text"] = ""
def jouer(self):
self.affichage["text"] = ""
self.nombre = random.randrange(self.NB_MIN, self.NB_MAX+1)
print('nombre=', self.nombre)
# -----------------Debut du programme------------------
if __name__ == "__main__":
root = Window()
root.mainloop() |
Partager