Tkinter ( NameError )

Résolu
fabespa34 Messages postés 6 Date d'inscription vendredi 27 janvier 2012 Statut Membre Dernière intervention 9 mai 2012 - 7 mai 2012 à 22:53
fabespa34 Messages postés 6 Date d'inscription vendredi 27 janvier 2012 Statut Membre Dernière intervention 9 mai 2012 - 8 mai 2012 à 15:53
Bonjour, Voila mon problème, je déclare une variable "var_choix" à un endroit et quand j'ai besoin de récupérer la valeur stockée dedans, Python me dit qu'elle n'existe pas . Ce n'est pas la première fois que ça m'arrive et je n'arrive pas à résoudre le problème. Je débute dans les interfaces graphiques donc soyez indulgents même si le problème peut paraitre simple pour certains.
Voici le code :
from tkinter import *

root = Tk()
root.title("Mastermind")

fenetre1 Frame(root, width 100, height = 50)
fenetre1.pack()

couleurs Label(fenetre1, text """Couleurs possibles :
Vert, Rouge, Jaune, Bleu, Violet, Cyan, Orange, Blanc.""")
couleurs.pack()

def Couleur():
    valid.destroy()
    var_choix = StringVar()# Le problème c'est avec cette variable  
    Coul1 Label(fenetre1, text "Couleur 1 : ")
    Coul1.pack()
    choix_vert Radiobutton(fenetre1, text "vert", variable = var_choix, value = "vert")
    choix_vert.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_rouge Radiobutton(fenetre1, text "rouge", variable = var_choix, value = "rouge")
    choix_rouge.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_jaune Radiobutton(fenetre1, text "jaune", variable = var_choix, value = "jaune")
    choix_jaune.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_bleu Radiobutton(fenetre1, text "bleu", variable = var_choix, value = "bleu")
    choix_bleu.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_violet Radiobutton(fenetre1, text "violet", variable = var_choix, value = "violet")
    choix_violet.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_cyan Radiobutton(fenetre1, text "cyan", variable = var_choix, value = "cyan")
    choix_cyan.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_orange Radiobutton(fenetre1, text "orange", variable = var_choix, value = "orange")
    choix_orange.pack(side = LEFT)
    coul1 = var_choix.get()
    choix_blanc Radiobutton(fenetre1, text "blanc", variable = var_choix, value = "blanc")
    choix_blanc.pack(side = LEFT)
    coul1 = var_choix.get()
        
    fenetre2 Frame(root, width 100, height = 50)
    fenetre2.pack()

    var_choix2 = StringVar()# Avec celle là,

    Coul2 Label(fenetre2, text "Couleur 2 : ")
    Coul2.pack(side = TOP)
    choix_vert2 Radiobutton(fenetre2, text "vert", variable = var_choix2, value = "rouge")
    choix_vert2.pack(side  = LEFT)
    choix_rouge2 Radiobutton(fenetre2, text "rouge", variable = var_choix2, value = "rouge")
    choix_rouge2.pack(side = LEFT)
    choix_jaune2 Radiobutton(fenetre2, text "jaune", variable = var_choix2, value = "jaune")
    choix_jaune2.pack(side = LEFT)
    choix_bleu2 Radiobutton(fenetre2, text "bleu", variable = var_choix2, value = "bleu")
    choix_bleu2.pack(side = LEFT)
    choix_violet2 Radiobutton(fenetre2, text "violet", variable = var_choix2, value = "violet")
    choix_violet2.pack(side = LEFT)
    choix_cyan2 Radiobutton(fenetre2, text "cyan", variable = var_choix2, value = "cyan")
    choix_cyan2.pack(side = LEFT)    
    choix_orange2 Radiobutton(fenetre2, text "orange", variable = var_choix2, value = "orange")
    choix_orange2.pack(side = LEFT)
    choix_blanc2 Radiobutton(fenetre2, text "blanc", variable = var_choix2, value = "blanc")
    choix_blanc2.pack(side = LEFT)

    fenetre3 Frame(root, width 100, height = 50)
    fenetre3.pack()

    var_choix3 = StringVar()Avec celle ci,

    Coul3 Label(fenetre3, text "Couleur 3 : ")
    Coul3.pack(side = TOP)
    choix_vert3 Radiobutton(fenetre3, text "vert", variable = var_choix3, value = "rouge")
    choix_vert3.pack(side  = LEFT)
    choix_rouge3 Radiobutton(fenetre3, text "rouge", variable = var_choix3, value = "rouge")
    choix_rouge3.pack(side = LEFT)
    choix_jaune3 Radiobutton(fenetre3, text "jaune", variable = var_choix3, value = "jaune")
    choix_jaune3.pack(side = LEFT)
    choix_bleu3 Radiobutton(fenetre3,text "bleu", variable = var_choix3, value = "bleu")
    choix_bleu3.pack(side = LEFT)
    choix_violet3 Radiobutton(fenetre3, text "violet", variable = var_choix3, value = "violet")
    choix_violet3.pack(side = LEFT)
    choix_cyan3 Radiobutton(fenetre3, text "cyan", variable = var_choix3, value = "cyan")
    choix_cyan3.pack(side = LEFT)    
    choix_orange3 Radiobutton(fenetre3, text "orange", variable = var_choix3, value = "orange")
    choix_orange3.pack(side = LEFT)
    choix_blanc3 Radiobutton(fenetre3, text "blanc", variable = var_choix3, value = "blanc")
    choix_blanc3.pack(side = LEFT)

    fenetre4 Frame(root, width 100, height = 50)
    fenetre4.pack()

    var_choix4 = StringVar()# Et avec celle ci aussi

    Coul4 Label(fenetre4, text "Couleur 4 : ")
    Coul4.pack(side = TOP)
    choix_vert4 Radiobutton(fenetre4, text "vert", variable = var_choix4, value = "rouge")
    choix_vert4.pack(side  = LEFT)
    choix_rouge4 Radiobutton(fenetre4, text "rouge", variable = var_choix4, value = "rouge")
    choix_rouge4.pack(side = LEFT)
    choix_jaune4 Radiobutton(fenetre4, text "jaune", variable = var_choix4, value = "jaune")
    choix_jaune4.pack(side = LEFT)
    choix_bleu4 Radiobutton(fenetre4,text "bleu", variable = var_choix4, value = "bleu")
    choix_bleu4.pack(side = LEFT)
    choix_violet4 Radiobutton(fenetre4, text "violet", variable = var_choix4, value = "violet")
    choix_violet4.pack(side = LEFT)
    choix_cyan4 Radiobutton(fenetre4, text "cyan", variable = var_choix4, value = "cyan")
    choix_cyan4.pack(side = LEFT)    
    choix_orange4 Radiobutton(fenetre4, text "orange", variable = var_choix4, value = "orange")
    choix_orange4.pack(side = LEFT)
    choix_blanc4 Radiobutton(fenetre4, text "blanc", variable = var_choix4, value = "blanc")
    choix_blanc4.pack(side = LEFT)

    fenetre5 Frame(root, width 10, height = 50)
    fenetre5.pack()

    valid1 Button(fenetre5, text "Valider ma combinaison.", command = validJoueur2)
    valid1.pack()

def validJoueur2():
    frame1 Frame(root, width 100, height = 50)
    frame1.pack()
    coul1 = var_choix.get()
    coul2 = var_choix2.get()
    coul3 = var_choix3.get()
    coul4 = var_choix4.get()

    essai Label(frame1, text "Couelur 1 : {}, Couelur 2 : {}, Couelur 3 : {} ,Couelur 4 : {}.".format(coul1, coul2, coul3, coul4))
    essai.pack()
        
debut Label(fenetre1, text "Joueur 1, composez votre code ( une couleur differente pour chaque pion ).")
debut.pack()

valid Button(fenetre1, text "Ok!", command = Couleur)
valid.pack()


root.mainloop()

Merci.

2 réponses

lespinx Messages postés 106 Date d'inscription lundi 9 octobre 2006 Statut Membre Dernière intervention 23 novembre 2022 77
8 mai 2012 à 15:45
Bonjour,

Je suppose que vous avez déjà trouvé la solution.
Toute variable décrite dans le corps d'une fonction à une portée locale

Les variable var_choix, var_choix2, var_choix3, var_choix4 ne sont pas visibles depuis les autres fonctions.
L'instruction "global" permet de les rendre visibles.
def Couleur():
    global var_choix, var_choix2, var_choix3, var_choix4
    valid.destroy()
    var_choix = StringVar()# Le problème c'est avec cette variable
.......	
3
fabespa34 Messages postés 6 Date d'inscription vendredi 27 janvier 2012 Statut Membre Dernière intervention 9 mai 2012
8 mai 2012 à 15:53
D'accord..., j'avoue que je ne connaissais pas cette fonction. Mais c'est super car je me suis heurté plusieurs fois au même problème et j'ai donc commencé plusieurs programmes cela va donc tous me les débloquer.
Merci beaucoup.
0
Rejoignez-nous