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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| # -*- coding: cp1252 -*-
from os import chdir,listdir
from random import *
from Tkinter import *
import sys
from lxml import etree
# Création d'un dictionnaire permettant d'ouvrir les fiches et de ne pas ouvrir deux fois la meme fiche
listeFiche={1:('Joseph_Henry.xml',True),2:('Nikola_Tesla.xml',True)}
#tex=IntVar()
# r1=Radiobutton(fen1,text="1",value="1")
# r2=Radiobutton(fen1,text="2",value="2")
# r3=Radiobutton(fen1,text="3",value="3")
# r4=Radiobutton(fen1,text="4",value="4")
# r1.pack(side=RIGHT)
# r2.pack(side=RIGHT)
# r3.pack(side=RIGHT)
# r4.pack(side=RIGHT)
def lireFiche(nomFiche):
#Lit le fichier XML et renvoit un arbre
tree = etree.parse(nomFiche)
#On récupère la racine de l'arbre, le noeud ayant balise <personnage>
fiche = tree.getroot()
return fiche
def getNom(fiche):
#Une fiche correspond à la racine du document, la balise <personnage>
#On récupère l'attribut "nom"
tree = etree.parse(nomFiche)
#On récupère la racine de l'arbre, le noeud ayant balise <personnage>
fiche = tree.getroot()
return fiche.get("nom")
def getPrenom(fiche):
#Une fiche correspond à la racine du document, la balise <personnage>
#On récupère l'attribut "prenom"
tree = etree.parse(nomFiche)
#On récupère la racine de l'arbre, le noeud ayant balise <personnage>
fiche = tree.getroot()
return fiche.get("prenom")
def afficherTexte():
bou3.destroy()
bou5.pack()
tree=etree.parse(nomFiche)
#On récupère la racine de l'arbre, le noeud ayant balise <personnage>
nodes=tree.xpath('/personnage/texte')
for node in nodes :
biographie= node.text
#can1 = Canvas(fen1,bg='blue',height=200,width=200)
#can1.pack(side=LEFT)
texte.insert(END, biographie)
texte.pack()
artiste.destroy()
def afficherQCM():
tree=etree.parse(nomFiche)
fiche=tree.getroot()
bou5.destroy()
tex3.pack()
texte.destroy()
tex1.destroy()
tree=etree.parse(nomFiche)
#On récupère la racine de l'arbre, le noeud ayant balise <personnage>
nodess=tree.xpath('/personnage/QCM')
for node in nodess :
QCM= node.text
#can1 = Canvas(fen1,bg='blue',height=200,width=200)
#can1.pack(side=LEFT)
question.insert(END, QCM)
question.pack()
artiste.destroy()
indiceQCM=0
indiceQCMcourant = 0
#On initialise les variables aux valeurs par défaut
reponses = []
quizz=""
indiceBonneReponse= 0
difficulte=""
i=0
#On parcourt les enfants du noeud racine
for node in fiche:
#Si le noeud courant est de type <QCM>
if node.tag == "QCM":
#Si c'est le bon indice de QCM
if indiceQCMcourant==indiceQCM:
#On récupère le texte dans la balise <QCM>, c'est le texte de la question
#la méthode strip() permet d'enlever les tabulation, espaces et retours à la ligne en début et fin de texte.
#Vous devrez l'utiliser à chaque fois que vous récupererez un texte dans une balise
quizz=node.text.strip()
#On récupère la valeur de l'attribut "difficulté"
difficulte=node.get("difficulte")
#Garde en mémoire l'indice de la réponse courante lors du parcours de la fiche, pour avoir l'indice de la bonne réponse
indiceReponse= 0
#Parcours des enfants de QCM, les noeuds de type <reponse>
for reponse in node:
#Si c'est bien un noeud de type réponse (théoriquement si la fiche est bien écrite ce test n'est pas necessaire)
if reponse.tag == "reponse":
#On ajoute à la fin de la liste le texte contenu dans la balise <reponse>
#Ici aussi on utilise strip()
answer = reponse.text.strip()
Label(fen1, text = "Réponse :").pack()
retour = IntVar()
Radiobutton(fen1, variable=retour, text= answer, value = i).pack()
#Si la réponse a l'attribut "correcte" égal à "oui"
if reponse.get("correcte")=="oui":
#On stocke l'indicee de la bonne réponse
indiceBonneReponse = indiceReponse
#On incremente l'indice de la réponse courante
i = i+1
#On incrémente l'indice courant de QCM
indiceQCMcourant+=1
#Label(fen1, text = "difficulté :").place(x=50,y=50)
#Label(fen1, text = difficulte).place(x=125,y=50)
bou6 = Button(fen1, text='Confirmer reponse', command=lambda : resultat(retour.get(),indiceBonneReponse))
bou6.pack()
return retour.get,indiceBonneReponse
def resultat(selection,indiceBonneReponse) :
fen2=Tk()
fen1.destroy()
if selection == indiceBonneReponse :
resultat='Bonne reponse!!!'
else :
resultat='Mauvaise reponse !!!'
Label(fen2, text=resultat).pack()
def afficherFiche():
#chdir(GC://Antoine/Info")
#nomFiche=("Joseph_Henry.xml")
fiche=lireFiche(nomFiche)
content=getPrenom(fiche)+' '+ getNom(fiche)
artiste.insert(END, content)
artiste.pack()
bou3.pack()
bou2.destroy()
tex1.pack(side=TOP)
return texte
# FENETRE PRINCIPALE
fen1 = Tk()
# création des widgets "esclaves" :
def TirageAleatoire():
n = randint(1,2)
if listeFiche[n][1] :
listeFiche[n] = (listeFiche[n][0],False)
return listeFiche[n][0]
else :
return TirageAleatoire()
chdir("C:\Users\Guillaume\Desktop\ALGO\Fiches-QCM\Fiche-xml")
nomFiche = TirageAleatoire()
#lireFiche(nomFiche)
difficulte="noire"
bou1 = Button(fen1,text='Quitter',command=fen1.destroy)
bou1.pack(side=BOTTOM)
bou3 = Button(fen1, text='Afficher Biographie', command=afficherTexte)
bou5 = Button(fen1, text='Afficher QCM', command=afficherQCM)
bou2 = Button(fen1, text='Afficher Fiche', command=afficherFiche)
bou2.pack()
tex1 = Label(fen1, text='Lire attentivement ce texte.', fg='black')
tex2 = Label(fen1, text='Cocher la bonne réponse.', fg='black')
tex3 = Label(fen1, text='Repondre a la question.', fg='black')
artiste=Text(fen1, width=16, height=4, bg="grey")
texte=Text(fen1, width=130, height=30)
question=Text(fen1, width=40, height=4, bg='grey')
fen1.mainloop() |
Partager