Bonjour,

Cette fois il ne s'agit pas d'un exercice de M. Swinnen, mais d'un petit programme pour moi.

Le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
from dictio import*
from  tkinter import*
from random import*
 
 
 
class Appli_Revis(Frame) :
    def __init__(self) :
        Frame.__init__(self)
        self.master.title('Module de révision')
 
        Button(self,text="Next",width=15,borderwidth=5,relief='raised',
               command=self.another).grid(row=0,column=2,padx=3,pady=3)
        Button(self,text="Answer",width=15,borderwidth=5,relief='raised',
               command=self.answer).grid(row=2,column=2,padx=3,pady=3)
        Button(self,text="Grammar",width=15,borderwidth=5,relief='raised',
               command=self.grammar).grid(row=4,column=2,padx=3,pady=3)
        Button(self,text="To revise",width=15,borderwidth=5,relief='raised',
               command=self.revise).grid(row=6,column=2,padx=3,pady=3)
        Button(self,text="Quit",width=15,borderwidth=5,relief='raised',
               command=self.quit).grid(row=8,column=2,padx=3,pady=3)
 
        Label(self,text="To translate").grid(row=1,column=0,padx=3,pady=3)
        self.lab_trans = Label(self,width=40)
        self.lab_trans.grid(row=1,column=1,padx=3,pady=3)
 
        Label(self,text="Proposal").grid(row=3,column=0,padx=3,pady=3)
        self.case_proposal = Entry(self,width=40)
        self.case_proposal.grid(row=3,column=1,padx=3,pady=3)
 
        Label(self,text="Answer").grid(row=5,column=0,padx=3,pady=3)
        self.lab_ans = Label(self,width=40)
        self.lab_ans.grid(row=5,column=1,padx=3,pady=3)
 
        Label(self,text="Counter").grid(row=7,column=0,padx=3,pady=3)
        self.lab_counter = Label(self,width=15)
        self.lab_counter.grid(row=7,column=1,padx=3,pady=3)
 
        self.list_fran = []
        self.list_ang = []
        self.dictio_revis = {}
        self.c = 0
 
        self.liste_fiches = ["Le futur avec will.txt","Past simple.txt"]
 
        self.pack()
 
    def another(self) :
        "Proposition d'un mot/d'une phrase en français, à traduire"
        self.lab_ans.configure(text='')
 
        for fran,ang in voca.items() :
            self.list_fran.append(fran)
            self.list_ang.append(ang)
 
        self.nbre = len(self.list_fran)-1
        self.i = randrange(self.nbre)
        self.lab_trans.configure(text=self.list_fran[self.i])
 
    def answer(self) :
        """Traduction du mot/de la phrase choisie, incrémentation du nbre
d'erreur, et ajout des éléments à revoir au dictionnaire"""
        self.lab_ans.configure(text=self.list_ang[self.i])
 
        proposal = self.case_proposal.get()
 
        if proposal != self.list_ang[self.i] :
            self.dictio_revis[self.list_fran[self.i]] = self.list_ang[self.i]
            self.c += 1
            self.lab_counter.configure(text=str(self.c))
 
    def revise(self) :
        """Affichage des éléments à revoir"""
        for fran,ang in self.dictio_revis.items() :
            print(fran,":",ang)
 
    def grammar(self) :
        "Affiche une fiche de révision conjugaison/grammaire au hasard"
        nombre = len(self.liste_fiches)-1
        r = randrange(nombre)
        choix = self.liste_fiches[r]
        fichier = open(choix,'r')
        lec = fichier.read()
        print(lec)
        fichier.close()
 
 
 
if __name__ == "__main__" :
 
    appli = Appli_Revis()
    appli.mainloop()
 
    appli.destroy()
Il fonctionne , j'ai simplement 3 petites questions :
1 - dans ma méthode "another", pour générer aléatoirement un mot à traduire, je découpe le dictionnaire en listes, je n'ai pas su faire autrement. Mais peut-être est-il possible d'utiliser randrange sur un dictionnaire ? Je n'ai pas su comment faire, puisqu'avec un dictionnaire on ne peut pas afficher le Xème élément.
2 - pour mon bouton "grammar", j'affiche dans le shell ma fiche de grammaire/conjugaison. Existe-t-il un module Python qui permette d'ouvrir réellement le fichier, dans son logiciel ? (j'ai cherché sur G, je n'ai rien trouvé qui semble faire ce genre d'opérations)
3 - quand je clique sur mon bouton "quitter", il me quitte le programme mais une fenêtre vide reste à l'écran. C'est parce que je créé un objet Frame directement ? Comment remédier à cela ?