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 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
| from tkinter import *
from tkinter import messagebox
from random import randint
from random import shuffle
# qs est une liste de dictionnaires ( chacun des ces dictionnaires a trois champs )
qs = [
{'question': "Quelle est la couleur du cheval blanc d'Henri IV ?",
'correcte': "blanc",
'incorrecte': "gris"},
{'question': "2 + 2 = ?",
'correcte': "4",
'incorrecte': "42"},
{'question': "Quel est l'antonyme de synonyme ?",
'correcte': "antonyme",
'incorrecte': "homonyme"},
{'question': "Qui a écrit 'La ballade des Pendus' ?",
'correcte': "François Villon",
'incorrecte': "François Rabelais"},
{'question': "De quel fleuve l'Erne est-elle un affluent ?",
'correcte': "La Loire",
'incorrecte': "La Seine"}]
def question(q,bouton1, bouton2):
global a
a=randint(0,1)
enonce=Label(root,text="",font ="Arial 14")
enonce.configure(text="Voici votre question : " + qs[q]["question"])
enonce.pack()
if a==0:
bouton1=Radiobutton(root, text=qs[q]["correcte"], variable=retour, value=1,font ="Arial 14",indicatoron=1,command=correction).pack(anchor=CENTER)
bouton2=Radiobutton(root, text=qs[q]["incorrecte"], variable=retour, value=2,font ="Arial 14",indicatoron=1,command=correction).pack(anchor=CENTER)
else:
bouton2=Radiobutton(root, text=qs[q]["incorrecte"], variable=retour, value=2,font ="Arial 14",indicatoron=1,command=correction).pack(anchor=CENTER)
bouton1=Radiobutton(root, text=qs[q]["correcte"], variable=retour, value=1,font ="Arial 14",indicatoron=1,command=correction).pack(anchor=CENTER)
def correction():
global score
print(retour.get())
reponse = retour.get()
print("correction de la"+ str(reponse),a)
if reponse == 1:
print("Bonne reponse")
score+=1
else:
print("Mauvaise réponse")
global n
n+=1
if n<len(t):
i=t[n]
question(i,"c","d")
else:
truc=Label(root,text="Fin du questionnaire. Votre taux de réussite est de : {} %".format (100*score//len(qs)),font ="Arial 14")
truc.pack()
#######################
# debut du programme principal
######################
root = Tk()
root.geometry("800x500")
score=0
a=0 # cette variable est globale elle pourra être consultée dans toute fonction mais ne pourra pas être modifiée
# sauf si dans certaines fonctions qui désirent la modifier on met global a
retour=IntVar() # variable spécifique au module tkinter sera utilisée pour le boutonradio
valider = 0
n=0
t=list(range(0,len(qs))) # on touille la liste des questions
shuffle(t)
question(t[0],"a","b")
root.mainloop() |
Partager