NameError: name 'ma_variable' is not defined
Résolu
gdnc78
-
gdnc78 -
gdnc78 -
Bonjour,
J'essaie de créer une application de quizz sur les départements français. Une première fenêtre demande, à l'aide de boutons, si je veux faire le test sur le nom des départements ou sur les numéros.
Si je choisis "par numéro", j'affiche une nouvelle fenêtre avec une Entry qui va être ma réponse.
Quand je vérifie dans mon programme si la réponse est bonne, j'ai le message :
NameError: name 'nom_tape' is not defined
Quelqu'un peut-il m'expliquer mon erreur ?
je vous mets le code ci-dessous :
from tkinter import * import os import random with open("departements_francais.txt", "r") as file: choix_dep = random.choice(file.readlines()).split(",") no_dep_a_trouver = choix_dep[0] nom_dep_a_trouver = choix_dep[1] file.close() class fenetres(): def __init__(self): self.window = Tk() self.window.title("Les departements français") self.window.geometry("540x260") self.window.config(background='#2ccaef') # on ajoute un titre champ_label = Label(self.window, text="QUIZ : les départements français", font=("Arial", 28), bg='#2ccaef', borderwidth=15) champ_label.pack() # on ajoute le bouton "par nom" dans la frame de gauche left_frame = Frame(self.window, width=270, height=260, bg='green') button_nom = Button(left_frame, text="par nom", font=("Courrier, 18"), bg='green', width=10, height=2, relief=SUNKEN, borderwidth=2, state=ACTIVE, command = self.par_nom) button_nom.pack(pady=20) left_frame.pack(side=LEFT, expand=1) # on ajoute le bouton "par numéro" dans la frame de droite right_frame = Frame(self.window, width=270, height=260, bg='red') button_numero = Button(right_frame, text="par numéro", font=("Courrier, 18"), bg="red", width=10, height=2, relief = SUNKEN, borderwidth = 2, state = ACTIVE, command = self.par_numero) button_numero.pack(pady=20) right_frame.pack(side=RIGHT, expand=1) ## afficher la fenetre self.window.mainloop() def choix_par_numero(self): self.window2 = Tk() self.window2.title("Les departements français par numéro") self.window2.geometry("540x260") self.window2.config(background='#2ccaef') label_window = Label(self.window2, text="QUIZ : ", font=("Arial", 28), bg='#2ccaef',borderwidth=15) label_window.pack() label_no_dep = Label(self.window2, text="quel est le département N° : ", font=("Courrier, 20"), bg='#49e2ca', fg='blue') label_no_dep.pack(pady=10, padx=10) no_dep = Label(self.window2, text=choix_dep[0], font=("Courrier, 20"), bg='#49e2ca', fg='blue') no_dep.pack(pady=10, padx=10) nom_tape = Entry(self.window2, font=("Courrier, 20"), bg='#49e2ca', fg='blue') nom_tape.pack(pady=10, padx=10) button_valider_nom = Button(self.window2, text="valider", font=("Courrier, 20"), bg="#4065A4", command=self.valider_nom) button_valider_nom.pack(pady=10, padx=10) ## afficher message d'erreur mess_err = Label(self.window2, text=" ", font=("Arial", 18), bg="#2ccaef") mess_err.pack(side=BOTTOM) ## afficher la fenetre self.window2.mainloop() def valider_nom(self): if nom_tape.get() == choix_dep[1]: print("good job") # self.window.destroy() # self.choix_par_numero() else: print("essaie encore") # mess_err['text'] = 'essaie encore' def par_numero(self): self.window.destroy() self.choix_par_numero() def choix_par_nom(): self.window3 = Tk() self.window3.title("Les departements français par nom") self.window3.geometry("540x260") self.window3.config(background='#2ccaef') left_frame = Frame(self.window3, width=390, height=300, bg='#43e1e0') label_no_dep = Label(fen_par_nom, text="quel est le N° du département : ", font=("Courrier, 20"), bg='#49e2ca', fg='white') label_no_dep.pack(pady=10, padx=10) nom_dep = Label(fen_par_nom, font=("Courrier, 20"), bg='#49e2ca', fg='white') nom_dep.pack(pady=10, padx=10) no_dep = Entry(fen_par_nom, font=("Courrier, 20"), bg='#49e2ca', fg='white') no_dep.pack(pady=10, padx=10) button_valider_no = Button(fen_par_nom, text="quitter", font=("Courrier, 20"), bg="#4065A4", command=self.valider_no) button_valider_no.pack(pady=10, padx=10) def valider_no(self): no_tape = input() if no_tape == no_dep_a_trouver: mess_err['text'] = "good job" else: mess_err['text'] = 'essaie encore' def par_nom(self): self.window.destroy() self.choix_par_nom() sart = fenetres()
Macintosh / Firefox 108.0
merci beaucoup