Bonjour,

Je me présente vite fait, je suis élève de terminal S spécialité ISN, comme bon nombre de personne ici, je viens chercher de l'aide. Au passage, merci aux anciens sujets de ce forum qui m'ont permis de régler d'autres soucis, mais là je ne trouve a priori pas de solution.

Comme plusieurs autres élèves passant par là, je prépare un projet de fin d'année, pour faire court, je réalise un programme en Python permettant la création et la modification de page HTML formant un jeu (ne cherchez pas trop je pense ^^), jusqu'ici tout va plutôt bien, j'ai dans un premier temps réalisé chaque partie (Création, Suppression, Renommer etc...) séparément dans un langage Python simple, puis je l'ai retranscrit dans du Tkinter pour avoir quelque chose de plus présentable que la console. J'ai donc déjà plutôt bien avancé en ayant la possibilité de Visionner les Jeux existants, pouvoir Supprimer, Renommer ou encore Créer un Jeu, j'en suis donc à la Modification. La Modification suit quasiment le même modèle que la partie d'Ajout d'un Jeu, à l'exception que les champs "Nom du Jeu", "Nom du Niveau", "Texte" ou encore l'image de fond seront prés rempli puisque déjà existant.

Mon problème est le suivant :

Le module d'Ajout simple est comme ceci : (Les lignes qui m'intéresse dans ce problème se situe au ligne 117 à 120)
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
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
from tkinter.filedialog import *
from tkinter import *
import os,shutil
Fond=""             #Accueil l'image de fond
 
 
 
def Save(Jeu,Niveau,Fond,Texte,fenetreAjouter):
    Jeu=Jeu.get()
    Niveau=Niveau.get()
    Texte=Texte.get(1.0,END)
    if Jeu=="" or Niveau=="":
        Alerte()
    else :
        Chemin="Jeux/"+Jeu
        ###Vérification si le nom n'est pas déjà existant
        Verif=os.path.exists(Chemin)
        if Verif==True :
            ConfirmerAvis=Tk()
            ConfirmerAvis.title('Confirmation')
            ConfirmerAvis.resizable(width=False,height=False)
            Label(ConfirmerAvis,text='Voulez vous effacer le jeu "'+Jeu+'" déjà existant ?').pack()
            BoutonRem=Button(ConfirmerAvis,text="Remplacer",command=lambda:Supprimer(Jeu,Chemin,ConfirmerAvis,fenetreAjouter))
            BoutonRem.pack(side=LEFT)
            Button(ConfirmerAvis,text="Annuler",command=ConfirmerAvis.destroy).pack(side=RIGHT)
            ConfirmerAvis.mainloop()
        if Verif==False :
            os.mkdir(Chemin)
            fenetreAjouter.destroy()
        ##Copier-Coller des éléments de base (Fond blanc bruité, style css de la page...)
        shutil.copyfile("systeme/StyleCSS/style.css",Chemin+"/style.css")
        shutil.copyfile("systeme/Images/bg1.jpg",Chemin+"/bg1.jpg")
        ##Remplissage de la page
        Contenu=open("systeme/PageType/top1.txt","r").read()+Niveau+" - "+Jeu+open("systeme/PageType/top2.txt","r").read()+Jeu+open("systeme/PageType/top3.txt","r").read()+Niveau+open("systeme/PageType/top4.txt","r").read()
        NiveauClean=Clean(Niveau) #Enlève accents, espaces etc...
        shutil.copyfile(Fond,Chemin+"/"+NiveauClean+"bg.png")   #Copie l'image choisi et le colle dans le dossier jeu en le renommant
        Contenu=Contenu+NiveauClean+"bg.png"+open("systeme/PageType/top5.txt","r").read()+"<p>"+Texte+"</p>"+open("systeme/PageType/bottom1.txt","r").read()
        Creation=open(Chemin+"/"+NiveauClean+".html","w")
        Creation.write(Contenu)
        Creation.close()
        ##Cette partie créait un fichier facilitant la modification ultérieur
        Modification=Jeu+'\n'+Niveau+'\n'+NiveauClean+'\n'+Fond+'\n'+Texte
        Modif=open(Chemin+"/"+NiveauClean+"modif"+".txt","w")
        Modif.write(Modification)
        Modif.close()
 
 
def Supprimer(Jeu,Chemin,ConfirmerAvis,fenetreAjouter):
    doc=os.listdir(Chemin)
    for i in range(len(doc)):
        os.remove(Chemin+"/"+doc[i])
    os.rmdir(Chemin)
    os.mkdir(Chemin)
    ConfirmerAvis.destroy()
    fenetreAjouter.destroy()
 
def Clean(Niveau):        #Nettoie le nom de ses accents et autres caractères spéciaux
    Nomfichier=""
    for i in range(len(Niveau)) :
        Caractere=Niveau[i]
        if Caractere in ["/",":","*","?",'"',"<",">","|","\ "," ","'","-","+"] :
            Nomfichier=Nomfichier
        else :
            if Caractere in ["é","è","ê","ë"] :
                Nomfichier=Nomfichier+"e"
            else :
                if Caractere in ["à","â","ã"] :
                    Nomfichier=Nomfichier+"a"
                else :
                    if Caractere in ["ù","û","ü"] :
                        Nomfichier=Nomfichier+"u"
                    else :
                        if Caractere in ["î","ï"] :
                            Nomfichier=Nomfichier+"i"
                        else :
                            if Caractere in ["ô","ö","õ"] :
                                Nomfichier=Nomfichier+"o"
                            else :
                                if Caractere in ["ñ"] :
                                    Nomfichier=Nomfichier+"n"
                                else :
                                    Nomfichier=Nomfichier+Caractere
    Niveau=Nomfichier
    return Niveau
 
def SelectImage():
    global Fond         #la fonction 'global' permet de traiter les variables (ici 'Fond') de manière globale et non local (dans tout le programme et non seulement dans la 'def')
    Fond=askopenfilename(title="Ouvrir une image",filetypes=[('png files','.png'),('all files','.*')])
    return Fond
 
def Alerte():
    Alert=Tk()
    Alert.title("Données manquantes")
    Alert.resizable(width=False,height=False)
    Label(Alert,text="Attention :\nCertains champs textes sont obligatoires, notamment:\n• Nom du Jeu\n• Nom du Niveau").pack(side=LEFT,padx=10,pady=10)
    Alert.mainloop()
 
 
 
#####Fenetre Principale#####
 
fenetreAjouter=Tk()
fenetreAjouter.title("Histoire dont vous êtes le héro - Créer un Nouveau Jeu")
fenetreAjouter.resizable(width=False,height=False)
 
Label(fenetreAjouter,text="Ajouter un nouveau Jeu",font=('bold')).pack(side=TOP,padx=200)
 
Frame(fenetreAjouter,height=20).pack()
 
##Nom du Jeu
NomDuJeu=Frame(fenetreAjouter)
NomDuJeu.pack(side=TOP)
Label(NomDuJeu,text="Nom du Jeu :").pack(side=LEFT,padx=5,pady=5)
Frame(NomDuJeu,width=520).pack()    #Séparation/ajusteur de taille/Facilite le placement
EntreeJeu=Frame(fenetreAjouter)
EntreeJeu.pack(side=TOP)
Message=StringVar()
Message.set("Salut !")
Jeu=Entry(EntreeJeu,textvariable=Message,width=40)
Jeu.pack(side=LEFT,padx=5)
Frame(EntreeJeu,width=352).pack()   #Séparation/ajusteur de taille/Facilite le placement
 
Frame(fenetreAjouter,height=15).pack()  #Séparation
 
##Nom du niveau principal/premier niveau
NiveauFond=Frame(fenetreAjouter)
NiveauFond.pack()
CadreDuNiveau=Frame(NiveauFond)
CadreDuNiveau.pack(side=LEFT)
CadreNiveau=Frame(CadreDuNiveau)
CadreNiveau.pack()
Label(CadreNiveau,text="Nom du Niveau :").pack(side=LEFT,padx=5,pady=5)
Frame(CadreNiveau,width=198).pack()
EntreeNiveau=Frame(CadreDuNiveau)
EntreeNiveau.pack()
Niveau=Entry(EntreeNiveau,width=40)
Niveau.pack(side=LEFT,padx=5)
Frame(EntreeNiveau,width=50).pack()
Frame(CadreDuNiveau,width=304,height=25).pack()
##Image de fond
CadreImage=Frame(NiveauFond)
CadreImage.pack(side=LEFT)
CadImage=Frame(CadreImage)
CadImage.pack()
Label(CadImage,text="Image de Fond :").pack(side=LEFT,padx=5,pady=5)
Frame(CadImage,width=203).pack()
Parcourir=Button(CadImage,text="Parcourir...",command=SelectImage)
Parcourir.pack(side=RIGHT,padx=5,pady=5)
CadreFond=Frame(CadreImage)
CadreFond.pack()
Frame(CadreFond,width=204,height=45).pack(side=LEFT,padx=5)
Frame(CadreFond,width=4).pack()
 
Frame(fenetreAjouter,height=15).pack()  #Séparation
 
##Texte
CadreTexte=Frame(fenetreAjouter)
CadreTexte.pack()
CadTexte=Frame(CadreTexte)
CadTexte.pack()
Label(CadTexte,text="Texte :").pack(side=LEFT,padx=5,pady=5)
Frame(CadTexte,width=560).pack()
ChampsTexte=Frame(CadreTexte)
ChampsTexte.pack()
Texte=Text(ChampsTexte,width=74,height=8,wrap='word')
Texte.pack(side=LEFT,padx=5)
Frame(ChampsTexte,width=4).pack()
 
Frame(fenetreAjouter,height=15).pack()  #Séparation
 
CadreBouton=Frame(fenetreAjouter)
CadreBouton.pack(side=RIGHT)
Button(CadreBouton,text="Annuler",command=fenetreAjouter.destroy).pack(side=RIGHT,padx=5,pady=5)
Button(CadreBouton,text="Sauvegarder",command=lambda:Save(Jeu,Niveau,Fond,Texte,fenetreAjouter)).pack(side=RIGHT,padx=5,pady=5)
 
 
 
fenetreAjouter.mainloop()
Si on le lance, tout va bien, l'Entry du Nom du Jeu est effectivement rempli par "Salut !"

En revanche, lorsque je passe le tout dans une 'def', comme ceci (permettant de l'incorporer au reste du 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
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
from tkinter.filedialog import *
from tkinter import *
import os,shutil
Fond=""             #Accueil l'image de fond
 
 
 
def Save(Jeu,Niveau,Fond,Texte,fenetreAjouter):
    Jeu=Jeu.get()
    Niveau=Niveau.get()
    Texte=Texte.get(1.0,END)
    if Jeu=="" or Niveau=="":
        Alerte()
    else :
        Chemin="Jeux/"+Jeu
        ###Vérification si le nom n'est pas déjà existant
        Verif=os.path.exists(Chemin)
        if Verif==True :
            ConfirmerAvis=Tk()
            ConfirmerAvis.title('Confirmation')
            ConfirmerAvis.resizable(width=False,height=False)
            Label(ConfirmerAvis,text='Voulez vous effacer le jeu "'+Jeu+'" déjà existant ?').pack()
            BoutonRem=Button(ConfirmerAvis,text="Remplacer",command=lambda:Supprimer(Jeu,Chemin,ConfirmerAvis,fenetreAjouter))
            BoutonRem.pack(side=LEFT)
            Button(ConfirmerAvis,text="Annuler",command=ConfirmerAvis.destroy).pack(side=RIGHT)
            ConfirmerAvis.mainloop()
        if Verif==False :
            os.mkdir(Chemin)
            fenetreAjouter.destroy()
        ##Copier-Coller des éléments de base (Fond blanc bruité, style css de la page...)
        shutil.copyfile("systeme/StyleCSS/style.css",Chemin+"/style.css")
        shutil.copyfile("systeme/Images/bg1.jpg",Chemin+"/bg1.jpg")
        ##Remplissage de la page
        Contenu=open("systeme/PageType/top1.txt","r").read()+Niveau+" - "+Jeu+open("systeme/PageType/top2.txt","r").read()+Jeu+open("systeme/PageType/top3.txt","r").read()+Niveau+open("systeme/PageType/top4.txt","r").read()
        NiveauClean=Clean(Niveau) #Enlève accents, espaces etc...
        shutil.copyfile(Fond,Chemin+"/"+NiveauClean+"bg.png")   #Copie l'image choisi et le colle dans le dossier jeu en le renommant
        Contenu=Contenu+NiveauClean+"bg.png"+open("systeme/PageType/top5.txt","r").read()+"<p>"+Texte+"</p>"+open("systeme/PageType/bottom1.txt","r").read()
        Creation=open(Chemin+"/"+NiveauClean+".html","w")
        Creation.write(Contenu)
        Creation.close()
        ##Cette partie créait un fichier facilitant la modification ultérieur
        Modification=Jeu+'\n'+Niveau+'\n'+NiveauClean+'\n'+Fond+'\n'+Texte
        Modif=open(Chemin+"/"+NiveauClean+"modif"+".txt","w")
        Modif.write(Modification)
        Modif.close()
 
 
def Supprimer(Jeu,Chemin,ConfirmerAvis,fenetreAjouter):
    doc=os.listdir(Chemin)
    for i in range(len(doc)):
        os.remove(Chemin+"/"+doc[i])
    os.rmdir(Chemin)
    os.mkdir(Chemin)
    ConfirmerAvis.destroy()
    fenetreAjouter.destroy()
 
def Clean(Niveau):        #Nettoie le nom de ses accents et autres caractères spéciaux
    Nomfichier=""
    for i in range(len(Niveau)) :
        Caractere=Niveau[i]
        if Caractere in ["/",":","*","?",'"',"<",">","|","\ "," ","'","-","+"] :
            Nomfichier=Nomfichier
        else :
            if Caractere in ["é","è","ê","ë"] :
                Nomfichier=Nomfichier+"e"
            else :
                if Caractere in ["à","â","ã"] :
                    Nomfichier=Nomfichier+"a"
                else :
                    if Caractere in ["ù","û","ü"] :
                        Nomfichier=Nomfichier+"u"
                    else :
                        if Caractere in ["î","ï"] :
                            Nomfichier=Nomfichier+"i"
                        else :
                            if Caractere in ["ô","ö","õ"] :
                                Nomfichier=Nomfichier+"o"
                            else :
                                if Caractere in ["ñ"] :
                                    Nomfichier=Nomfichier+"n"
                                else :
                                    Nomfichier=Nomfichier+Caractere
    Niveau=Nomfichier
    return Niveau
 
def SelectImage():
    global Fond         #la fonction 'global' permet de traiter les variables (ici 'Fond') de manière globale et non local (dans tout le programme et non seulement dans la 'def')
    Fond=askopenfilename(title="Ouvrir une image",filetypes=[('png files','.png'),('all files','.*')])
    return Fond
 
def Alerte():
    Alert=Tk()
    Alert.title("Données manquantes")
    Alert.resizable(width=False,height=False)
    Label(Alert,text="Attention :\nCertains champs textes sont obligatoires, notamment:\n• Nom du Jeu\n• Nom du Niveau").pack(side=LEFT,padx=10,pady=10)
    Alert.mainloop()
 
 
def AJOUTER():
    #####Fenetre Principale#####
 
    fenetreAjouter=Tk()
    fenetreAjouter.title("Histoire dont vous êtes le héro - Créer un Nouveau Jeu")
    fenetreAjouter.resizable(width=False,height=False)
 
    Label(fenetreAjouter,text="Ajouter un nouveau Jeu",font=('bold')).pack(side=TOP,padx=200)
 
    Frame(fenetreAjouter,height=20).pack()
 
    ##Nom du Jeu
    NomDuJeu=Frame(fenetreAjouter)
    NomDuJeu.pack(side=TOP)
    Label(NomDuJeu,text="Nom du Jeu :").pack(side=LEFT,padx=5,pady=5)
    Frame(NomDuJeu,width=520).pack()    #Séparation/ajusteur de taille/Facilite le placement
    EntreeJeu=Frame(fenetreAjouter)
    EntreeJeu.pack(side=TOP)
    Message=StringVar()
    Message.set("Salut !")
    Jeu=Entry(EntreeJeu,textvariable=Message,width=40)
    Jeu.pack(side=LEFT,padx=5)
    Frame(EntreeJeu,width=352).pack()   #Séparation/ajusteur de taille/Facilite le placement
 
    Frame(fenetreAjouter,height=15).pack()  #Séparation
 
    ##Nom du niveau principal/premier niveau
    NiveauFond=Frame(fenetreAjouter)
    NiveauFond.pack()
    CadreDuNiveau=Frame(NiveauFond)
    CadreDuNiveau.pack(side=LEFT)
    CadreNiveau=Frame(CadreDuNiveau)
    CadreNiveau.pack()
    Label(CadreNiveau,text="Nom du Niveau :").pack(side=LEFT,padx=5,pady=5)
    Frame(CadreNiveau,width=198).pack()
    EntreeNiveau=Frame(CadreDuNiveau)
    EntreeNiveau.pack()
    Niveau=Entry(EntreeNiveau,width=40)
    Niveau.pack(side=LEFT,padx=5)
    Frame(EntreeNiveau,width=50).pack()
    Frame(CadreDuNiveau,width=304,height=25).pack()
    ##Image de fond
    CadreImage=Frame(NiveauFond)
    CadreImage.pack(side=LEFT)
    CadImage=Frame(CadreImage)
    CadImage.pack()
    Label(CadImage,text="Image de Fond :").pack(side=LEFT,padx=5,pady=5)
    Frame(CadImage,width=203).pack()
    Parcourir=Button(CadImage,text="Parcourir...",command=SelectImage)
    Parcourir.pack(side=RIGHT,padx=5,pady=5)
    CadreFond=Frame(CadreImage)
    CadreFond.pack()
    Frame(CadreFond,width=204,height=45).pack(side=LEFT,padx=5)
    Frame(CadreFond,width=4).pack()
 
    Frame(fenetreAjouter,height=15).pack()  #Séparation
 
    ##Texte
    CadreTexte=Frame(fenetreAjouter)
    CadreTexte.pack()
    CadTexte=Frame(CadreTexte)
    CadTexte.pack()
    Label(CadTexte,text="Texte :").pack(side=LEFT,padx=5,pady=5)
    Frame(CadTexte,width=560).pack()
    ChampsTexte=Frame(CadreTexte)
    ChampsTexte.pack()
    Texte=Text(ChampsTexte,width=74,height=8,wrap='word')
    Texte.pack(side=LEFT,padx=5)
    Frame(ChampsTexte,width=4).pack()
 
    Frame(fenetreAjouter,height=15).pack()  #Séparation
 
    CadreBouton=Frame(fenetreAjouter)
    CadreBouton.pack(side=RIGHT)
    Button(CadreBouton,text="Annuler",command=fenetreAjouter.destroy).pack(side=RIGHT,padx=5,pady=5)
    Button(CadreBouton,text="Sauvegarder",command=lambda:Save(Jeu,Niveau,Fond,Texte,fenetreAjouter)).pack(side=RIGHT,padx=5,pady=5)
 
 
 
    fenetreAjouter.mainloop()
 
 
fenetre=Tk()
 
AjouterJeu=Button(fenetre,text='Ajouter',command=AJOUTER)
AjouterJeu.pack(padx=5,pady=5)
 
fenetre.mainloop()
Si on teste ce second code, l'Entry n'est plus rempli par le '.set' et le 'textvariable', ce qui est bien embêtant pour un module de Modification ^^" Je ne comprends donc pas le problème auquel je fais fasse, j'ai voulu faire un test pour voir si c'est le passage dans une 'def' qui implique systématiquement ce problème, mais avec le test suivant, il semblerait que le problème soit autre chose que le simple passage du code dans une 'def' :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
from tkinter import *
 
def Test():
    fenetre=Tk()
    Message=StringVar()
    Message.set("Salut !")
    Jeu=Entry(fenetre,textvariable=Message,width=40)
    Jeu.pack(padx=5,pady=5)
    fenetre.mainloop()
 
Test()
Merci d'avance pour votre aide !